I am trying to correct uneven background illumination in the text document using Morphological top-hat operation.
After applying Top hat, result I get is worse for analysis than the original image.
img=rgb2gray(imread("test2.png"));
se=strel("square",100);
imshow(imtophat(img,se));
Could you please advice what i am doing wrong ?
The top hat filter works for light text on a black background. You can either invert the image (imcomplement) before applying the top hat, or use imbothat instead.
In either case, the output is identical: light text on a black background. You can invert the image after the operation to get dark text on a white background.
Related
I have already tried imfill(img) but that doesn't work at all.
I have noticed that imfill works for this purpose only for images in which the object's boundary is complete and not broken like the image i am taking .
For this particular image do the following:
Prepend a white row to the image to close the contour.
Fill the contour.
Remove the helper row.
Anything else requires more information and examples.
Matlab documentation states that "a hole is a set of background pixels that cannot be reached by filling in the background from the edge of the image". In this case any pixel can be reached from the edge because its boundary is not complete. Therefore, technically there is no hole in the image you posted.
I am having trouble to change the bar color, I want it to be white in the middle, and red at the edge. Looking at matlab's description
if I do:
bar(y,'FaceColor','w','EdgeColor','r','LineWidth',1)
It should give me the above. However, when I actually run it, it only give me white graph.
Update: my y is:
y=zeros(1,5000); y(3000)=1; y(4000)=1;
Using the above, I got....
With so many bars, Matlab probably has trouble differentiating edge ('EdgeColor') and fill ('FaceColor') of each. After all, each complete bar is less than a screen pixel.
I suggest you use white edge and colored fill. That works for me. It's as if 'FaceColor' had precedence over 'EdgeColor'.
bar(y,'FaceColor','r','EdgeColor','w','LineWidth',1)
Or better yet: replace each bar by a line, that is, use stem:
stem(y,'r','marker','none')
I have a to make an application that recognizes fruits. So far i have made that you can crop the image and get the color of the fruit you want. Now i am trying to get roundness of the fruit but i need the fruit to be black and the background to be white so i can find area and roundness value. This is my code so far for that part :
crop_temp = rgb2gray(crop);
threshold = graythresh(crop_temp);
bw = im2bw(crop_temp,threshold);
imshow(bw)
Crop i get passed when i crop the image. The problem gets when the fruit has a camera flash and that part stays white.
An example image is this lemon picture:
The problem is the white area in the lemon stays white after the code but i want it so that the whole lemon is black. But not just the lemon, but for other fruits to.
The problem is the white area in the lemon stays white after the code but i want it so that the whole lemon is black. But not just the lemon, but for other fruits to.
Yeah and how can you make so that the fruit is white and the background is black.
I am new to image processing so don't jump on me. I just can't find specific stuff for this.
Try this one:
fbw = ones(size(bw))-imfill(ones(size(bw))-bw);
imshow(fbw)
A brute force approach would be to check every white pixel in your image, and see if it is boxed in by black pixels in both the X and Y directions, turning it black if this is the case. This would take care of blobs inside your fruit, and shouldn't give you too many false-positives unless your fruit are strangely shaped, or you have a lot of noise around the edges of your image.
You can start by practicing with this Matlab demo, segmenting (and counting) rice in an image. In particular the part where the background is estimated.
Also helpful will be reading on Otsu's method and these two questions on background/foreground estimation on SO and DSP which take local statistics into account.
I'm working with this excellent example of converting an image to grayscale: Convert Image to B&W problem CGContext - iPhone Dev
However, for my purposes, I would like to have only pure black and pure white left in the image.
It appears that to do so, I need to pass a black and white color space to the recolor method using a call:
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(/*black and white name*/);
However, I was unable to find the proper iOS color space names. What I found was from Mac, and the "color space names" referenced from the iOS docs does not point anywhere.
How can I properly create a black and white CGColorSpaceRef?
Thank you!
I am not familiar with a black and white only color space but what you can do is calculate the total average RGB value from all the pixels (lets call it totalAvg) and use it as a threshold. Meaning for each pixel if its rgb average is greater than the calculated totalAvg than set it to pure white, otherwise set it to pure black.
I agree it is a bit of more work but thats whay I can think of unless you find the colorspace you are looking for.
You might try creating a gray color space, then creating an indexed color space with two colors (black and white, obviously) and using that.
I've converted a colored photo to black and white, and bolded the edges. Now i need to convert it back to its original color with the bolded edges. Is there any function in matlab which allows me to do so?
Once you remove the colour from an image, there is no possible way to automatically put it back. You're basically reducing a set of 16,777,216 colours to a set of 256 - on average each shade of grey has 65,536 equivalent colours, and without the original image there's no way to guess which it could be.
Now, if you were to take the bolded lines from your black-and-white image and paint them on top of the original coloured image, that might end up producing what you're looking for.
If what you are trying to do is to use some filter over the B/W image and then use that with the original color. I suggest you convert your image to a color space with Lightness channel that suits your needs (for example L*a*b* if you need the ligtness to be uniformly distributed regarding human recognition of differences) and apply your filter only over the Lightness channel.