Ask Your Question

How to remove unwanted corners in images?

asked 2016-07-16 19:37:33 -0600

bb12jo gravatar image

updated 2016-07-18 21:56:02 -0600


I am trying to rid of the corners in an image where I will be combining that image with three other images. The corners have been blocking a part of the image next to it. The images are in the shape of a trapezoid because I used the perspective shift in opencv. Here is the single image:C:\fakepath\image.png and here is the attempt at combing them: C:\fakepath\blank.png

Here is my code:

import cv2
import numpy as np
import matplotlib.pyplot as plt
def autocrop(image, threshold=0):
#Crops the bottom part of the image, but not the corners
    if len(image.shape) == 3:
        flatImage = np.max(image, 2)
        flatImage = image
    assert len(flatImage.shape) == 2

    rows = np.where(np.max(flatImage, 0) > threshold)[0]
    if rows.size:
        cols = np.where(np.max(flatImage, 1) > threshold)[0]
        image = image[cols[0]: cols[-1] + 1, rows[0]: rows[-1] + 1]
        image = image[:1, :1]

    return image
img1 = cv2.imread("testBird.png", 1) #cv.IMREAD_COLOR

image = np.zeros((700, 700, 4), np.uint8)
src = np.array([[0,200],[480,200],[480,360],[0,360]],np.float32)
dst = np.array([[0,0],[480,0],[300,210],[180,210]],np.float32)
cvters = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
t, s = cv2.threshold(cvters, 1, 255, cv2.THRESH_BINARY)
rs, gs, bs = cv2.split(img1)
finals = cv2.merge((rs, gs, bs, s))

M = cv2.getPerspectiveTransform(src, dst)
warp = cv2.warpPerspective(finals.copy(), M, (480, 360))

two = warp.copy()

three = warp.copy()
four = warp.copy()
image[:warp.shape[0], 100:warp.shape[1]+100]= warp
twoR = cv2.getRotationMatrix2D((two.shape[1]/2,two.shape[0]/2),90,1)
twoD = cv2.warpAffine(two,twoR,(two.shape[1],two.shape[0]))

twoD = autocrop(twoD)

image[100:twoD.shape[0]+100, 60:twoD.shape[1]+60]= twoD

cv2.imwrite('blank.png', image)

I have tried making the pixels transparent using the alpha channel, but I never got it to work(You can still see that I have the alpha channel split and merged). I have tried cropping which works for the bottom half of the image, but not the corners. I have tried doing a contour crop but that didn't work although I felt like I might have done something wrong. I have tried to fix this issue for over a week now so any help would be greatly, greatly appreciated!

edit retag flag offensive close merge delete



just as a sidenote: please do not alias cv2 to cv, there was an older (completely incompatible) version with that name, and you're confusing everyone like that !

berak gravatar imageberak ( 2016-07-16 22:37:33 -0600 )edit

Thanks for the heads up! Fixed.

bb12jo gravatar imagebb12jo ( 2016-07-16 22:41:09 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-19 02:22:30 -0600

dandur gravatar image

If you make your images with equal size you can blend them together with simple addition. Alpha channel is not need for this.

edit flag offensive delete link more



Nevermind, fixed the issue. Thanks!!!!!!

bb12jo gravatar imagebb12jo ( 2016-07-20 17:03:02 -0600 )edit

Question Tools

1 follower


Asked: 2016-07-16 19:37:33 -0600

Seen: 1,105 times

Last updated: Jul 19 '16