gpt4 book ai didi

python - 从给定4点坐标的图像中提取任意矩形补丁

转载 作者:行者123 更新时间:2023-12-02 16:37:14 27 4
gpt4 key购买 nike

给定图像中四个任意点的坐标(保证形成一个矩形),我想提取它们代表的面片并获得相同点的矢量化(平面)表示。我怎样才能做到这一点?

我看到了this问题的答案,并使用它可以找到所需的补丁。例如,给定该图像中绿色矩形的4个角的图像坐标:

我能够找到补丁并得到类似的信息:

使用以下代码:

p1 = (334,128)
p2 = (438,189)
p3 = (396,261)
p4 = (292,200)
pts = np.array([p1, p2, p3, p4])

mask = np.zeros((img.shape[0], img.shape[1]))

cv2.fillConvexPoly(mask, pts, 1)
mask = mask.astype(np.bool)

out = np.zeros_like(img)
out[mask] = img[mask]
patch = img[mask]

cv2.imwrite(img_name, out)

但是,问题在于,当以行优先顺序将图像作为矩阵读取时,我获得的 patch变量只是图像中属于该补丁的所有像素的数组。

我想要的是 patch变量应包含可以形成真实图像的顺序的像素,以便我可以对其执行操作。有没有我应该意识到的opencv函数可以帮助我做到这一点?

谢谢!

最佳答案

这是实现此方法的方法:

码:

    # create a subimage with the outer limits of the points
subimg = out[128:261,292:438]

# calculate the angle between the 2 'lowest' points, the 'bottom' line
myradians = math.atan2(p3[0]-p4[0], p3[1]-p4[1])
# convert to degrees
mydegrees = 90-math.degrees(myradians)

# create rotationmatrix
h,w = subimg.shape[:2]
center = (h/2,w/2)
M = cv2.getRotationMatrix2D(center, mydegrees, 1)
# rotate subimage
rotatedImg = cv2.warpAffine(subimg, M, (h, w))

结果:
enter image description here

接下来,通过删除所有100%黑色的行/列,可以轻松裁剪图像中的黑色区域。
最后结果:
enter image description here
码:
    # converto image to grayscale
img = cv2.cvtColor(rotatedImg, cv2.COLOR_BGR2GRAY)

# sum each row and each volumn of the image
sumOfCols = np.sum(img, axis=0)
sumOfRows = np.sum(img, axis=1)

# Find the first and last row / column that has a sum value greater than zero,
# which means its not all black. Store the found values in variables
for i in range(len(sumOfCols)):
if sumOfCols[i] > 0:
x1 = i
print('First col: ' + str(i))
break

for i in range(len(sumOfCols)-1,-1,-1):
if sumOfCols[i] > 0:
x2 = i
print('Last col: ' + str(i))
break

for i in range(len(sumOfRows)):
if sumOfRows[i] > 0:
y1 = i
print('First row: ' + str(i))
break

for i in range(len(sumOfRows)-1,-1,-1):
if sumOfRows[i] > 0:
y2 = i
print('Last row: ' + str(i))
break

# create a new image based on the found values
finalImage = rotatedImg[y1:y2,x1:x2]

关于python - 从给定4点坐标的图像中提取任意矩形补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55712015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com