Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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 image by itself and when I try to combine it with another image.[^]

Here is my code:
<pre lang="Python">
import cv2 as cv
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<pre lang="Python"><pre 
    if len(image.shape) == 3:
        flatImage = np.max(image, 2)
    else:
        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]
    else:
        image = image[:1, :1]

    return image
img1 = cv.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 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
t, s = cv.threshold(cvters, 1, 255, cv.THRESH_BINARY)
rs, gs, bs = cv.split(img1)
finals = cv.merge((rs, gs, bs, s))

M = cv.getPerspectiveTransform(src, dst)
warp = cv.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 = cv.getRotationMatrix2D((two.shape[1]/2,two.shape[0]/2),90,1)
twoD = cv.warpAffine(two,twoR,(two.shape[1],two.shape[0]))

twoD = autocrop(twoD)

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

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


I have struggled with this problem for many, many hours so any help would be greatly appreciated! Thanks!

What I have tried:

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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900