Have you any ideas how can I with python and OpenCV get subimages on original image by that mask? I need separated subimages of every white area.
Because it's not rects it's hard to get them separated.
I think you are looking for connectedComponentsWithStats(), which will give you connected components (i.e., one label per white area). The result will be a labeled image with a separate label for each component.
From this, it is easy to extract the part of the image with a specific label.
Related
I'm trying to make an indoor navigation and I need indoor map that robot can automatically navigate the way. I'm thinking of using image which have different colors for each place(certain section), and I want to know the way to get the coordinates of the certain colors. So that I can designate places to certain color area using that coordinates. I am currently using pycharm
How can I get the coordinates of each of the pink,purple, and yellow part?
RGB code of the colors are pink(255,128,255), yellow(255,255,0), purple(128,128, 255).
This is the image that I'll use
The solution to your problem will involve two main parts:
Detecting the color from input image
Converting the blob to a single coordinate.
Let's take the first problem. You can use cv2.inRange() with various colors, to get a binary mask for each of your marked squares in the input image.
Now you can use cv2.findContours on the binary mask(s) to detect the largest contour and the take it's mid-point or something.
Cropped Only character from Image and remove spaces
I am trying to remove extra spaces from left and right of character image through opencv .Every image has different dimension .Is there any way to crop or remove extra spaces from left and right only or fetch only character from image.
Under the assumption that all your images look like the examples provided you can go with HansHirse's solution.
Segment the image into fore- and background using a global threshold. Then find the extreme coordinates of foreground pixels to define the boundaries of your sub image.
Of course you can do both steps on the fly instead of doing two iterations over your image.
Another solution: Use a blob detector to find the character components and merge their bounding boxes.
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 a set of grayscale images, like this:
This is an example image as I cannot post the original image. Each image has an area with a texture, a pure white watermark (pos), and lots of unwanted black space.
Ideally this image should be cropped to:
The watermark can be slightly different in each image, but is always very thin pure white text.
The pictures can look very different, here is another example
this one only needs cropping on the left
another one:
this one needs to be cropped on top and bottom:
and another one
this one needs to be cropped at the top and right. Note that I left the watermark in this picture. Ideally the watermark would be removed as well, but I guess it is easier without.
here is a picture of the watermark how it looks in reality.
The images vary in size, but are usually large (over 2000x2000).
I am looking for a solution in python (cv2 maybe).
my first idea was to use something like this:
Python & OpenCV: Second largest object
but this solution code fails for me
I work in C# and C++ and don't work in python but can suggest you the logic.
You need to run two scan of the image, one row wise and other columns wise.
Since you said the unwanted part of image is always black, just read the pixel values in both scan. If the color of all the pixels in a certain row is black then you can elemminate or delete that row. Similar steps can be followed for column wise scanning.
Now we cannot just eleminate the rows and columns so easily, so just note down the redundant rows and columns and then you can crop your image using following code:( I will code in C# with emgucv library but it is easy to understand for python)
Mat original_image = new Mat();
Rect ROI = new Rect(x,y,width,height);
Mat image_needed_to_crop = new Mat(original_image,ROI);
This code just extracts only the region of interest from the original image.
I am currently trying very hard to figure out a way to make these four trapezoid images into one nice image. The final image should look something like this(used photoshop to make it):
That above image will be complied with four of these images:
The problem is that when I try to rotate and combine these images, the black surroundings come into the final image as well like this:
How am I supposed to rid of the blacked out area or make it transparent? I've tried using a mask but that only make the black area white instead. I have also tried using the alpha channel, but that didn't work(although maybe I was doing wrong). Any ideas on what I can do in OpenCV?
I did actually figure it out. I did it with these steps:
Create two SAME SIZED black backgrounds with numpy zeros
Put one image in each background where you want them(for me, it was left and top)
Then all you need to do is cv.add(first, second)
The reason it works is because black pixels are 0,0,0 so adding to a pixel that is, say, 25,62,34, the pixel doesn't change and thus rids of the black corner.