Imagemagick remove odd pixels from the image - image

I have a mortar image and it has some blackish pixels on the inside edges.
I need to make those blackish pixels colored same as the gray mortar color.
Look at the beginning of the image I have pointed out some blackish pixels on the edges

one (time consuming) solution is to switch colors to grey of every pixel that passes the following condition:
pixel is part of a component that is connected to a mortar but is not part of the mortar rectangle, this can be achieved with modifications to "Connected-component labeling" algorithm.
one way to do this modification is:
scan the image one time with Connected-component labeling algorithm (with threshold to contain grey and black colors pixels).
discard every component that doesnt contain a mortar (in your case this can be achieved with a demand of minimum pixels in a component).
scan the image a second time, each fixel that is on a line with an end and is black, switch color to grey.
this can be done in number of ways, one way is to search the line ends and then changes colors on all the line (you can flag the line ends in Connected-component labeling algorithm to save time).
hope this helps.

Related

Anti Aliasing - Alpha Image [Unity3D]

I have a system that removes the colour white (give or take a few shades), from an image and replaces it with an alpha channel. (The image is taken from the users phone camera, and tries to remove selected colouring)
This leaves harsh edges most of the time, and I want to know if it is possible to add some type of anti-aliasing on top.
The system works by taking in the image, and searching through each pixel data. If the pixel is white (or close), it will replace it with an alpha colour.
So I guess my question is, how do I make the edges less harsh. Thanks.
Anti aliasing is not what you are looking for. This takes care of effects caused by the limited resolution of your image. However, your problem is not related to resolution, you would still have it with infinite resolution.
What you need to do is when you find a white pixel, increase the transparency of the pixel itself and the pixels around it.
You can just include the four pixels immediately above, below, left or right of your white pixel, or you an choose any other shape, e.g. all pixels which lie inside a circle of given radius around the white pixel.
Also you can choose a function which determines how transparency is distributed over that shape. You can make everything half-transparent or you can decrease the effect towards the edges of that shape (though I don't think that this will be necessary).
Thus each pixel will receive transparency from several pixels around them. The resulting transparency must be computed from all these contributions. Simply multiplying them probably won't do, because you will have a hard time ever reaching alpha=0. You may however, interpret (255-alpha) as a measure of transparency, add all contributing transparencies and then convert back into alpha. Something like max (0, 255 - (255-a1) + (255-a2) ...).
It will be difficult to do this in-place, i.e. with just ony copy of the image. You might need an intermediate "image", where each pixel is associated with all transparency contributions from the pixels around it.

How to cut extra background in image?

Say I have an image that looks like this:
I fail to see what approach I could take in order to modify the picture so that all the background color surrounding the image is gone. So, a potential result would be this:
As you can see the white background has been cut out and now is about 2 pixels from the actual shoes.
I don't have just shoes, but I am looking for an algorithm that would let me do that. I am using Ruby and Minimagick, but I guess that first step would be to figure it out the algorithm that I could use.
EDIT: The background is not necessary white.
If I understand you right, this sounds like a simple task that doesn't need any fancy algorithms.
Find the background color of the image. One simple way to do that would be to just take the color of the pixel in, say, the top left corner. There are fancier ways you could use, but this will work for your example image.
Find the leftmost and rightmost columns containing a pixel of some color other than the background. Those columns will be the leftmost and rightmost columns of your cropped image.
Find the topmost and bottommost rows containing a pixel of some color other than the background. Those rows will be the topmost and bottommost rows of your cropped image.
Crop the image to the dimensions found above. If you want, you can adjust the dimensions to leave a border of any size you want around the image.
Use s-t minimal graph cuts algorithm from standard image processing library

Any ideas on how to remove small abandoned pixel in a png using OpenCV or other algorithm?

I got a png image like this:
The blue color is represent transparent. And all the circle is a pixel group. So, I would like to find the biggest one, and remove all the small pixel, which is not group with the biggest one. In this example, the biggest one is red colour circle, and I will retain it. But the green and yellow are to small, so I will remove them. After that, I will have something like this:
Any ideas? Thanks.
If you consider only the size of objects, use the following algorithm: labellize the connex components of the mask image of the objects (all object pixels are white, transparent ones are black). Then compute the areas of the connex components, and filter them. At this step, you have a label map and a list of authorized labels. You can read the label map and overwrite the mask image with setting every pixel to white if it has an authorized label.
OpenCV does not seem to have a labelling function, but cvFloodFill can do the same thing with several calls: for each unlabeled white pixel, call FloodFill with this pixel as marker. Then you can store the result of this step in an array (of the size of the image) by assigning each newly assigned pixel with its label. Repeat this as long as you have unlabellized pixels.
Else you can recode the connex component function for binary images, this algorithm is well known and easy to implement (maybe start with Matlab's bwlabel).
The handiest way to filter objects if you have an a priori knowledge of their size is to use morphological operators. In your case, with opencv, once you've loaded your image (OpenCV supports PNG), you have to do an "openning", that is an erosion followed by a dilation.
The small objects (smaller than the size of the structuring element you chose) will disappear with erosion, while the bigger will remain and be restored with the dilation.
(reference here, cv::morphologyEx).
The shape of the big object might be altered. If you're only doing detection, it is harmless, but if you want your object to avoid transformation you'll need to apply a "top hat" transform.

When using Photoshop/GIMP is it better to color->alpha and then resize, or vice versa?

I was making a circular icon with semi-transparency, so I started with a large filled-in circle with a black border, then I did white->alpha, and resized the image to my required size. Would it have made a difference if I resized first, and then did white->alpha?
Thanks.
Yes.
In general, whenever you are re-sampling, this will have an impact if you are using any anti-aliasing, or the resampling algorithm is something other than nearest-neighbor.
Try the following exercise for a visual example:
In both cases, create your circular icon.
Case 1:
Change white-center of the circle to alpha (0%, fully transparent).
Re-sample (ie: down-sample to 25%) the entire image using something other than nearest neighbour (ie: actually use antialiasing of some sort)
Paste a copy of the result over a red background.
You should only see black and red colors inside the circle when you zoom in, with a smooth transition from black-to-red.
Case 2:
Re-sample (ie: down-sample to 25%) the entire image using something other than nearest neighbour (ie: actually use antialiasing of some sort)
Change white-center of the circle to alpha (0%, fully transparent).
Paste a copy of the result over a red background.
You should see a black outer circle, with a bit of a white halo inside of it, then the red center, with a smooth black-to-white transition, and a sharp white-to-red transition. This will depend on the aggressiveness factor you set with the magic-wand tool you are likely using to auto-select the region you want to modify the alpha properties of.
Now repeat case 2, but disable any sort of anti-aliasing, and enforce the use of a nearest neighbour algorithm rather than bi-cubic spline, Hermite, Gaussian, etc. Your results will look very similar to case 1, except you won't see the smooth transition from black-to-red when you zoom in, you will just see a sharp black-to-red transition.
In general, you will get the best subjective quality when working on your images first, then re-sampling later. If you paste it as its own layer, then you still have all the image data available any none is lost, the image is just rendered smaller.

Edge Detection and transparency

Using images of articles of clothing taken against a consistent background, I would like to make all pixels in the image transparent except for the clothing. What is the best way to go about this? I have researched the algorithms that are common for this and the open source library opencv. Aside from rolling my own or using opencv is there an easy way to do this? I am open to any language or platform.
Thanks
If your background is consistend in an image but inconsistent across images it could get tricky, but here is what I would do:
Separate the image into some intensity/colour form such as YUV or Lab.
Make a histogram over the colour part. Find the most occuring colour, this is (most likely) your background (update) maybe a better trick here would be to find the most occuring colour of all pixels within one or two pixels from the edge of the image.
Starting from the eddges of the image, set all pixels that have that colour and are connected to the edge through pixels of that colour to transparent.
The edge of the piece of clothing is now going to look a bit ugly because it consist of pixels that gain their colour from both the background and the piece of clothing. To combat this you need to do a bit more work:
Find the edge of the piece of clothing through some edge detection mechanism.
Replace the colour of the edge pixels with a blend of the colour just "inside" the edge pixel (i.e. the colour of the clothing in that region) and transparent (if your output image format supports that).
If you want to get really fancy, you increase the transparency depending on how much "like" the background colour the colour of that pixel is.
Basically, find the color of the background and subtract it, but I guess you knew this. It's a little tricky to do this all automatically, but it seems possible.
First, take a look at blob detection with OpenCV and see if this is basically done for you.
To do it yourself:
find the background: There are several options. Probably easiest is to histogram the image, and the large number of pixels with similar values are the background, and if there are two large collections, the background will be the one with a big hole in the middle. Another approach is to take a band around the perimeter as the background color, but this seems inferior as, for example, reflection from a flash could dramatically brighten more centrally located background pixels.
remove the background: a first take at this would be to threshold the image based on the background color, and then run the "open" or "close" algorithms on this, and then use this as a mask to select your clothing article. (The point of open/close is to not remove small background colored items on the clothing, like black buttons on a white blouse, or, say, bright reflections on black clothing.)
OpenCV is a good tool for this.
The trickiest part of this will probably be at the shadow around the object (e.g. a black jacket on a white background will have a continuous gray shadow at some of the edges and where to make this cut?), but if you get this far, post another question.
if you know the exact color intensity of the background and it will never change and the articles of clothing will never coincide with this color, then this is a simple application of background subtraction, that is everything that is not a particular color intensity is considered an "on" pixel, one of interest. You can then use connected component labeling (http://en.wikipedia.org/wiki/Connected_Component_Labeling) to figure out seperate groupings of objects.
for a color image, with the same background on every pictures:
convert your image to HSV or HSL
determine the Hue value of the background (+/-10): do this step once, using photoshop for example, then use the same value on all your pictures.
perform a color threshold: on the hue channel exclude the hue of the background ([0,hue[ + ]hue, 255] typically), for all other channels include the whole value range (0 to 255 typically). this will select pixels which are NOT the background.
perform a "fill holes" operation (normally found along blob analysis or labelling functions) to complete the part of the clothes which may have been of the same color than the background.
now you have an image which is a "mask" of the clothes: non-zero pixels represents the clothes, 0 pixels represents the background.
this step of the processing depends on how you want to make pixels transparent: typically, if you save your image as PNG with an alpha (transparency) channel, use a logical AND (also called "masking") operation between the alpha channel of the original image and the mask build in the previous step.
voilĂ , the background disappeared, save the resulting image.

Resources