Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to remove unwanted corners in images?

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 single image:C:\fakepath\image.png and here is the attempt at combing them: C:\fakepath\blank.png

Here is my code:

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 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!

How to remove unwanted corners in images?

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 single image:C:\fakepath\image.png and here is the attempt at combing them: C:\fakepath\blank.png

Here is my code:

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

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

cv.imwrite('blank.png', 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!

How to remove unwanted corners in images?

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 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<pre lang="Python"><pre 
corners
    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 = 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!