水平放置图像中的对象

问题描述 投票:0回答:1

我有以不同角度定向的食物托盘图像。我想使所有托盘都水平放置。为此,我尝试使用霍夫变换找到纸盘的最长边缘,并计算其相对于图像边界的方向并旋转它。在极少数情况下,它可以正常工作。我想使其适用于所有图像。谁能帮我这个忙吗?我在下面的链接中附加了一些示例图像,并且还包括了我当前正在使用的代码。Link for images

def Enquiry(lis1): 
   return(np.array(lis1)) 

img = cv2.imread('path/to/image')
canny = cv.Canny(img, 100, 200)
minLineLength = 200
maxLineGap = 10
lines = cv.HoughLinesP(canny, 1, np.pi / 180, 100, minLineLength, maxLineGap)
if Enquiry(lines).size>=4:
   lines1 = lines[:,0,:]
   max_length = 0
   index = 0
   i = 0
   for x1, y1, x2, y2 in lines1:
       length = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
       if length > max_length:
          max_length = length
          index = i
          i += 1

   [x1,y1,x2,y2]=lines1[index]
   degree = math.atan(abs(y1-y2)/abs(x1-x2))
   angle = degree*180/np.pi
   H, W = img.shape[:2]
   rotation_matrix = cv.getRotationMatrix2D((W/2, H/2), -angle, 1)
   img_rotation = cv.warpAffine(img, rotation_matrix, (W, H))
   cv2.imwrite('rotated_image.jpg', img_rotation)
python opencv computer-vision orientation image-rotation
1个回答
0
投票

仅旋转不会有帮助。在测试图像中,托盘也有一些剪切和倾斜。

我建议的是从直线的交点找到拐角。至少3个角。然后找到托盘角和预期实际角之间的仿射变换。

© www.soinside.com 2019 - 2024. All rights reserved.