finding simple rectangles in image, save and replace them - python

How could I find simple rectangles in image such as:
save them in separate picture files and replace them by a simple description such as:

Turn the image to binary by assigning white where you see the color of a rectangle and black elsewhere.
Detect the connected components and determine their bounding box.
Now you can replace the content of the image in these bounding boxes.
Repeat for every rectangle color.
There must be a rule to tell which text to write in which box. But you did not state that.

I assume this is an RGB image.
One possible solution would be to use OpenCV or Pillow to convert the image into an array (3 channels). Then set a threshold for the colours.
For example, the red colour is RBG=(255,0,0). You could set a threshold of more than 200 for the first channel and less than 5 for the second and third channels.
Then you determine the coordinates of the localised area and get the resulting array.
Then, you convert this array to white. So, replace this array with a new array (having the same dimension) with the white colour RGB=rgb(255,255,255).
Since you already know the position of the localised rectangle, you can label the new image with PIL, for example.

Related

Python - fill colors of an image with closest non zero color

I have an image that is created by ray casting a bunch of vectors on to a mesh with a uv map (in blender). There are not enough vectors to completely cover the image so I'd like a way to fill the rest of the image with the closest non zero color. I've been looking into some techniques with convolutions in numpy etc but can't really find what I need, attached is an example of an image I'm working with - png with RGBA.
[Edited to add]
Possibly a better description of what I am trying to do:
for each pixel that doesn't have a cast color (ie black) I need to find the closest pixel with a cast color based on the distance away, not based on how similar the RGB values are.

crop image into individual parts

I used semantic segmentation to color code the different elements in an image shown below.
In Python, I want to crop the original image into many small images based on the colors of the second image, so that the sofa becomes one cropped part, the lamp becomes one.etc. The overlap of the pillows on the sofa can be ignored. Say I have a 3D array of an image, I want to separate that array into the individual colored sections, and apply the coordinate of those elements in cropping the original image. How should I achieve this?
You can do it like this:
find the number of unique colours in the segmented image - see here
iterate over that list of colours making that colour white and everything else black, then findContours() to get the bounding box and save the contents of that bounding box as a PNG.

Extract connected/neighboring regions in an image via a specific input colored pixel

I have an image -
I would like to get an output image that just has the highlighted red portions.
Output image -
How would I do this ?
What I am trying to do is isolate underline/strikethrough text decorations so that I can extract the text along with the text decoration information.
What I had in mind -
Remove all pixels (convert to white) that are not connected to a red
pixel (connected to red pixel via some other non-white pixel also counts). I tried using connected components but it seems I need to pass
in a binary image and this approach failed.
Using a sliding window to iterate through the pixels and if the
sliding window has a red pixel, don't do anything, if it doesn't,
turn all the pixel within the window - white. The sliding window size
will be adjusted based on trial and error.

How to draw a rectangular box(slit) on a region file using python-ds9

Its my first time to use python code to display ds9 images.I just display black and white image but I wanted to get a color image with two or more slit positioned on the image which passes at the center,plus the position angle and coordinates should not be manually typed coz I have many images.I expect something like this.As you see the slits are not at the center I need them all at the center but they are at different angle.I need your help as usual,thanks!

Crop out complementary regions from an image

I have coordinate regions of an image. I am cropping out images from those coordinates. Now, I need complimentary regions of what that is cut out, with respect to the original image. How do I go about using Pillow?
If you crop a region you basically create a new, smaller image.
A complementary operation to that would be to fill the region with some value you consider invalid or zero as you will still have an image of the original size. Technically you cannot remove a region from an image. you can just change or ignore it.
PIL.ImageDraw.Draw.rectangle(xy, fill=None, outline=None)
is something I found quickly. Maybe there is something better. Just crawl through the reference.

Categories

Resources