How shall I crop images with 2 colour borders in Python? - python

My original image was this:enter image description here
After I rotate them, I got this:
enter image description here
Therefore, I am currently having both black and white borders. I only want the middle part of the image, how can I crop them with Python?
I am new here, appreciate if anyone can help! Thanks in advance.

Related

Converting an RGB image to grayscale for a whole dataset in Python

enter image description hereI am new to python programming. i have to convert my dataset rgb images to grayscale then have to apply cycleGAN on that dataset.i am using zelda levels dataset.I have no idea how and haven't found many useful things from looking through the internet. If someone could point me in the right direction, so I can figure out how to either change it to a one channel image or grayscale that would be great.
remove this red marked line and it should be fine.

How to save a contour of an image as another image?

I am trying to take contour of an image and rotate that contour alone. so I want to store the contour as a separate image. please help me with the code and thanks in advance:)

Alpha channel become red when imread

I have .png image like this
But when I want to read and show the image, the alpha channel area become fully red like this
My python code for read and show are like this.
imageA = cv2.imread('img/lenna.png')
cv2.imshow("image A", imageA)
Why the transparent area become red when I want to just read and show it?
Can I make it still transparent?
Thank you.

Rotate an image without black area

When we use some image processing library to rotate an image, the rotated image will always contains some black area. For example, I use the following python code to rotate an image:
from scipy import misc
img = misc.imread('test.jpg')
img = misc.imrotate(img,15)
misc.imsave('rotated.jpg')
The image is as follows:
My question is: how can I rotate an image without producing black area. I believe there exists some interpolation method to compensate for the missing area, which makes the image more natural.
It will be appreciated if anyone can provide a python code to achieve my task.
If you want to 'clone' or 'heal' the missing areas based on some part of the background, that's a complex problem, usually done with user intervention (in tools like Photoshop or GIMP).
Alternatives would be to fill the background with a calculated average colour - or just leave the original image. Neither will look 'natural' though.
The only approach that will work for all images will be to crop the rotated image to the largest rectangle within the rotated area. That will achieve your objective of having no black areas and looking natural, but at the cost of reducing the image size.
isnt there a simple paint fill function in your "some image library" ?, simple do that at all 4 corner pixels and then make it white or so.

Opencv: How to stitch four trapezoid images to make a square 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.

Categories