【问题标题】:Opencv homography does not produce the required tranformationOpencv 单应性不产生所需的转换
【发布时间】:2019-10-27 21:47:30
【问题描述】:

我正在尝试沿对象的边缘转换图像(这里的对象是书)。使用精明的边缘检测,我正在检测边缘,并从分数矩阵中,基于像素值,我选择位于边缘的随机 4 个坐标进行转换。但转变并不像想象的那样。有什么问题/我错过了什么?

首先,我切出了图像的一部分。然后应用canny边缘检测并根据我自己的条件随机选择4个边缘坐标点为: 我的原图是:

对于实验,我根据自己的需要切出了:

这张图片的大小(61,160)

现在我需要对上面的图像进行变换,使书的边缘平行于水平轴。

img = cv2.imread('download1.jpg',0)
edges = cv2.Canny(img,100,200)
print(img.shape)
plt.show()
plt.imshow(img,cmap='gray')

l=[]
y_list=[]
k=1
for i in range (0,img.shape[0]):
  for j in range (0,img.shape[1]):
    if (edges[i][j]==255) and k<=4 and i>31 and j not in y_list:
      l.append([j,i])
      y_list.append(j)
      k+=1
      break

得到边缘检测图像为:

l列表的内容是

[[49 32]
 [44 33]
 [40 34]
 [36 35]]

然后将list lt给出的目的地点设置为:

[[49 61]
 [44 60]
 [40 61]
 [36 60]]

然后找出单应矩阵并用它来找出翘曲透视为:

h, status = cv2.findHomography(l,lt)
im_out = cv2.warpPerspective(img, h, (img.shape[1],img.shape[0]))

但它不会产生所需的结果!得到的输出图像为:

【问题讨论】:

    标签: python python-3.x opencv homography


    【解决方案1】:

    我遇到了类似的问题,这就是我解决它的方法(实际上与您的方法非常相似),只是我使用了 get rotation matrix 而不是 homografy:

    1. 读取图片
    2. 边缘检测器
    3. 获取所有线的霍夫线(在特定区间内具有倾斜度)

      lines = cv.HoughLinesP(img, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
      
    4. 获取线的平均倾斜度,因为在我的情况下,我有很多平行线可以用作 参考文献,这样我就能得到更好的结果

       for line in lines:
           x1,y1,x2,y2 = line[0]
           if (x2-x1) != 0:
               angle = math.atan((float(y2-y1))/float((x2-x1))) * 180 / math.pi
           else:
               angle = 90
           #you can skip this test if you have no info about the lines you re looking for
           #in this case offset_angle is = 0
           if min_angle_threshold <= angle <= max_angle_threshold:
              tot_angle = tot_angle + angle
              cnt = cnt + 1
       average_angle = (tot_angle / cnt) - offset_angle 
      
    5. 应用反向旋转

        center = your rotation center - probably the center of the image
        rotation_matrix = cv.getRotationMatrix2D(center, angle, 1.0)
        height, width = img.shape
        rotated_image = cv.warpAffine(img, rotation_matrix, (width, height))
      
       #do whatever you want, then rotate image back
       counter_rotation_matrix = cv.getRotationMatrix2D(center, -angle, 1.0)
       original_image = cv.warpAffine( rotated_image, counter_rotation_matrix, (width, height))
      

    编辑:在此处查看完整示例:

        import math
        import cv2 as cv
    
        img = cv.imread('C:\\temp\\test_3.jpg',0)
        edges = cv.Canny(img,100,200)
        lines = cv.HoughLinesP(edges[0:50,:], 1, np.pi/180, 50, minLineLength=10, maxLineGap=10)
        tot_angle = 0
        cnt = 0
        for line in lines:
            x1,y1,x2,y2 = line[0]
            if (x2-x1) != 0:
                angle = math.atan((float(y2-y1))/float((x2-x1))) * 180 / math.pi
            else:
                angle = 90
    
            if -30 <= angle <= 30:
                tot_angle = tot_angle + angle
                cnt = cnt + 1
        average_angle = (tot_angle / cnt)
        h,w = img.shape[:2]
        center = w/2, h/2
        rotation_matrix = cv.getRotationMatrix2D(center, average_angle, 1.0)
        height, width = img.shape
        rotated_image = cv.warpAffine(img, rotation_matrix, (width, height))
        cv.imshow("roto", rotated_image)
        #do all your stuff here, add text and whatever
        #...
        #...
        counter_rotation_matrix = cv.getRotationMatrix2D(center, -average_angle, 1.0)
        original_image = cv.warpAffine( rotated_image, counter_rotation_matrix, (width, height))
        cv.imshow("orig", original_image)
    

    旋转

    ]1

    counter_rotated

    ]2

    编辑:

    如果您想应用单应性(与简单的旋转不同,因为它还应用透视变换),请在代码下方使其工作:

    #very basic example, similar to your code with fixed terms
    l  = np.array([(11,32),(43,215),(142,1),(205,174)])
    lt = np.array([(43,32),(43,215),(205,32),(205,215)])
    h, status = cv.findHomography(l,lt)
    im_out = cv.warpPerspective(img, h, (img.shape[1],img.shape[0]))
    

    以编程方式进行 - 对于“l”:也可以使用 houghlines 并找到 4 个角, 然后添加它们

    • 对于“lt”:为所有 4 个点找到一个“目的地”,例如使用底角作为参考

      lines = cv.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=150, maxLineGap=5)
      l = []
      for line in lines:
          x1,y1,x2,y2 = line[0]
      
          if (x2-x1) != 0:
              angle = math.atan((float(y2-y1))/float((x2-x1))) * 180 / math.pi
          else:
              angle = 90
          # consider only vertical edges
          if 60 <= angle:
              l.append((x1,y1))
              l.append((x2,y2))
              x_values.append(max(x1,x2)) 
              if len(y_values) == 0:
                  y_values.append(y1)
                  y_values.append(y2)
      l  = np.array(l)
      lt = np.array([(x_values[0],y_values[0]),(x_values[0],y_values[1]),(x_values[1],y_values[0]),(x_values[1],y_values[1])])
      

    然后像上面那样调用 findhomography 希望它足够清楚!

    3

    【讨论】:

    • 其实我想嵌入文字然后重新旋转,怎么做?
    • 我可以嵌入文本,但是如何重新旋转它以获取上一个图像?
    • 我认为您可以在旋转后的图像中添加文字,然后将其旋转回来(角度 *-1)
    • 我看到中心决定了图像旋转的方式,如何让图像旋转特定的边缘?如何确定旋转中心
    • 我想做一条平行于横轴的边,怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多