簡體   English   中英

如何始終使用 minAreaRect,c++ 將圖像旋轉到 180 度

[英]How to rotate the image to 180 degree always using minAreaRect, c++

我的圖像;

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

要求:

在此處輸入圖片說明

我無法理解軸是如何決定使圖像始終水平的。

算法:

  • 閱讀圖片
  • 查找外部輪廓
  • 繪制輪廓
  • 使用外部輪廓檢測 minArearect (邊界框對我沒有幫助)
  • 獲取旋轉矩陣並旋轉圖像
  • 從旋轉圖像中提取所需的補丁

我的代碼:

//read the image
img = imread("90.jpeg")
cv::Mat contourOutput = img.clone();

// detect external contour(images will have noise, although example images doesn't have)

std::vector<std::vector<cv::Point> > contours;
cv::findContours(contourOutput, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

int largest_area = 0;
int largest_contour_index = 0;
for (size_t i = 0; i < contours.size(); i++) {
    double area = contourArea(contours[i]);

// copy the largest contour index
    if (area > largest_area) {
        largest_area = area;
        largest_contour_index = i;
}
}
//draw contours
drawContours(img, contours, largest_contour_index, Scalar(255, 0, 0),
             2);
// detect minimum area rect to get the angle and centre
cv::RotatedRect box = cv::minAreaRect(cv::Mat(contours[largest_contour_index]));
// take the box angle
double angle = box.angle;
if (angle < -45) {
    box.angle += 90;
}

angle = box.angle;
// create rotation matrix
cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1);

// Apply the transformation
cv::Mat rotated;
cv::warpAffine(img, rotated, rot_mat, img.size(), cv::INTER_CUBIC);
cv::Size box_size = box.size;

if (box.angle < -45.)
    std::swap(box_size.width, box_size.height);
// get the cropped image
cv::Mat cropped;
cv::getRectSubPix(rotated, box_size, box.center, cropped);

// Display the image
namedWindow("image2", WINDOW_NORMAL);
imshow("image2", cropped);
waitKey(0);

這個想法是使用minAreaRect計算旋轉的邊界框角度,然后使用getRotationMatrix2DwarpAffine對圖像進行校正。 如果我們使用垂直圖像,最后一步是旋轉 90 度。 這是之前(左)和之后(右)以及旋轉角度的結果:

-39.999351501464844

38.52387619018555

1.6167902946472168

1.9749339818954468

我在 Python 中實現了它,但您可以將相同的方法應用於 C++

代碼

import cv2
import numpy as np

# Load image, grayscale, and Otsu's threshold
image = cv2.imread('4.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Compute rotated bounding box
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]

# Determine rotation angle
if angle < -45:
    angle = -(90 + angle)
else:
    angle = -angle
print(angle)

# Rotate image to deskew
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

# Vertical image so rotate to horizontal
h, w, _ = rotated.shape
if h > w:
    rotated = cv2.rotate(rotated, cv2.ROTATE_90_CLOCKWISE)

cv2.imshow('rotated', rotated)
cv2.imwrite('rotated.png', rotated)
cv2.waitKey()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM