I have an image with a solid background and three types of objects on it. The three objects are of different (but slightly varying sizes) and of different colors.
For example if there is a 40-60 pixel group that is black in color that grouping would be put in group A, if there is a 40-60 pixel group that is white in color that grouping would be put in group B and if there is a 90-110 pixel group that is black in color that grouping would be put in group C. Then the algorithm would need to count the number of objects in each group.
How would I go about creating an algorithm to do this (preferably using python)?
Thanks in advance.
The answaer to your problem is Canny edge detector.
You can find python implementation of it here.
Good luck!
A way which works is to binarize the image according to the color first.
For example in your case, to detect object A, you make all pixels which are not black white. Then you get an image with only two kinds of colors (black and white).
And you can use connected component detection to find all the groups of black pixels. If number of pixels in a group is large than 40, you can think it is an object A.
You make want to check out my matlab code Detect-Gray-Square
Related
I'm trying to determine the color hierarchy of some lines in an image, using the images library in python.
If you are not familiar with the images library, it represents an image as a list of lists (a matrix). Each item inside the inner list is a tuple with RGB values (e.g. (255, 255, 255)).
How would this be possible? The background color is known, the lines are always straight and in different colors.
A sample image can be seen here: https://imgur.com/J35IT9k
The answer for this image would be: white first, red second, green third and then the background color (gray) last.
The colors would be represented in tuples of RGB values of course
As you have been unable to provide any information or references to this mystery "images" library, I am suggesting a different approach.
If your images are always in a plus shape, i.e. axis-aligned and symmetrical, you can probably use a simple heuristic to find the layer ordering.
the image is symmetrical, so you only need to process the rows or the columns but not both
the grey pixels are a distraction, make them black (i.e. zero) so they contribute nothing
sum across the rows with np.sum(..., axis=1) so you find the horizontal lines, i.e. where the row sums to a large number
look at each line you find, and test the number of unique colours in it - if it is 1 (i.e. white in your diagram) it is top-most. If it is 2, it is the next layer down and so on.
I have an image that consists of small black dots, and in each dot's vicinity there is some noise that appears as grayish smudge.
I'm trying to use some sort of image processing in Python in order to find both the number of (correct) dots and the number of noise smudges, as well as calculate their paramaters (i.e size).
I was thinking of using some sort of contour detection with a certain threshold, since the dots' borders are more distinct, but perhaps there's a better way that I'm not familiar with.
Thank you for your help!
Use the Pillow module to analyze each pixel color and compare it against whether its RGB values added together (Assuming its only black and white) are:
Black: 0
Grey: 1-764
White: 765
Hope that helps
Input image
I need to group the region in green and get its coordinates, like this output image. How to do this in python?
Please see the attached images for better clarity
At first, split the green channel of the image, put a threshold on that and have a binary image. This binary image contains the objects of the green area. Start dilating the image with the suitable kernel, this would make adjacent objects stick to each other and become to one big object. Then use findcontour to take the sizes of all objects, then hold the biggest object and remove the others, this image would be your mask. Now you can reconstruct the original image (green channel only) with this mask and fit a box to the remained objects.
You can easily find the code each part.
I have an input image which consists of 3 colors.These colors are circular in shape and nested.
The image is similar to this :
https://www.google.ie/search?q=red+yellow+blue+nested+circles&client=ms-unknown&tbm=isch&tbs=rimg:CbQTsOKsM7yhIkCaDOdJHzqnN2Xk-DhItFHm0Zqt6wMB32Tm1CzyzQ7wrXERbVqngEyMBzO57J8UuHLak9WPqWfjV7kgvdJ47BJlKhIJmgznSR86pzcR8SW2ldYWlqIqEgll5Pg4SLRR5hG-6WlMFrVBvioSCdGaresDAd9kEVFfCyyB-AgqKhIJ5tQs8s0O8K0RT790ELynuK8qEglxEW1ap4BMjBHBPar4Jd2NtioSCQczueyfFLhyEY7iP_13IGcsOKhIJ2pPVj6ln41cRTMOWeqZE5oYqEgm5IL3SeOwSZREray5kAy-dzw%3D%3D&tbo=u&ved=0ahUKEwjWzfSC6IbXAhXFbBoKHW3GBrUQuIIBCCM#imgrc=5tQs8s0O8K32hM:
I will be working with several images like this.The imaage is always of the same thing.however dude to different camera , lighting , even printer differences , the actual color can vary. In that it will always be red yellow green in the order showen. By using HSV and thresholds I can easily determine upper and lower values for each color. However If I change to a different set of images theses values are no longer functional.
My idea to overcome this is to look for contours first in the image.
For each contour I would like to get an upper and lower threshold. Using a combination of canny , gaussian and contours I am able to draw contour around each color from testing this seems general enough for purpose.
Where I'm stuck is getting the threshold value from within the contours. Is this possible? Or is there simpler logic I am over looking to achieve this ?
At present I'm using python , but language is secondary.
Forget about the contours, they will make things harder than necessary.
A better approach could be by classifying the pixels using k-means. Initialize with at least three clusters, centered around green, yellow, red. Maybe one centered on white, for the background.
After convergence you should have the exact colors, together with segmentation.
https://en.wikipedia.org/wiki/K-means_clustering#Standard_algorithm
How to go from the image on the left to the image on the right programmatically using Python (and maybe some tools, like OpenCV)?
I made this one by hand using an online tool for clipping. I am completely noob in image processing (especially in practice). I was thinking to apply some edge or contour detection to create a mask, which I will apply later on the original image to paint everything else (except the region of interest) black. But I failed miserably.
The goal is to preprocess a dataset of very similar images, in order to train a CNN binary classifier. I tried to train it by just cropping the image close to the region of interest, but the noise is so high that the CNN learned absolutely nothing.
Can someone help me do this preprocessing?
I used OpenCV's implementation of watershed algorithm to solve your problem. You can find out how to use it if you read this great tutorial, so I will not explain this into a lot of detail.
I selected four points (markers). One is located on the region that you want to extract, one is outside and the other two are within lower/upper part of the interior that does not interest you. I then created an empty integer array (the so-called marker image) and filled it with zeros. Then I assigned unique values to pixels at marker positions.
The image below shows the marker positions and marker values, drawn on the original image:
I could also select more markers within the same area (for example several markers that belong to the area you want to extract) but in that case they should all have the same values (in this case 255).
Then I used watershed. The first input is the image that you provided and the second input is the marker image (zero everywhere except at marker positions). The algorithm stores the result in the marker image; the region that interests you is marked with the value of the region marker (in this case 255):
I set all pixels that did not have the 255 value to zero. I dilated the obtained image three times with 3x3 kernel. Then I used the dilated image as a mask for the original image (i set all pixels outside the mask to zero) and this is the result i got:
You will probably need some kind of method that will find markers automatically. The difficulty of this task depends heavily on the set of the input images. In some cases, the method can be really straightforward and simple (as in the tutorial linked above) but sometimes this can be a tough nut to crack. But I can't recommend anything because I don't know how your images look like in general (you only provided one). :)