使用 OpenCV 根据对象的方向旋转圆形对象图像

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

我是 OpenCV 的初学者,我有下面给出的图像,我想以图像中的文本水平的方式定位图像,所以我想根据中间矩形部分的方向旋转图像的图像。如何使用 OpenCV 检测图像的矩形部分。

(https://i.stack.imgur.com/oCFCL.jpg)

我尝试了某些阈值方法,但没有生成整齐的掩模。谁能建议一些根据对象方向旋转图像的方法。

谢谢

opencv opencv3.0 image-rotation image-thresholding
1个回答
0
投票

我的方法是检测文本的背景,该背景在图像的蓝色通道上显得非常弱。然后,使用边界矩形(水平)和最小区域矩形(旋转)的属性来创建旋转矩阵。代码是这样的:

im = cv2.cvtColor(cv2.imread("textrotation.jpg"), cv2.COLOR_BGR2RGB) # convert to rgb for easy plotting
r,g,b = cv2.split(im) # split to channels
_, thresholded = cv2.threshold(b, 50, 255, cv2.THRESH_BINARY_INV) # value of text box very low on blue channel, threshold there
contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # contouring
largestContour = max(contours, key = cv2.contourArea) # get biggest contour
x, y, w, h = cv2.boundingRect(largestContour) # get the bounding rectangle, which is horizontal
center = (x + w // 2, y + h // 2) # get the center
angle = cv2.minAreaRect(largestContour)[-1] # get angle of min area rect, since this one is rotated
rotationMatrix = cv2.getRotationMatrix2D(center, angle, 1.0) # create the rotation matrix
rotatedImage = cv2.warpAffine(im, rotationMatrix, (im.shape[1], im.shape[0]), flags=cv2.INTER_NEAREST) # rotate

结果如下所示:

随意根据整个贴纸(棕色)进行裁剪,这将是另一种方法,看起来与我发布的方法非常相似。虽然过滤棕色不太可靠...

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