OpenCV旋转图像精度

时间:2019-04-16 19:12:03

标签: c++ opencv

我在图像中有重叠的矩形。查找轮廓函数无法看到顶部矩形的边界,因为边缘重叠处对比度不高。但是,我可以可靠地发现图像中较小的矩形。顶部矩形的已知尺寸为1200x480像素。我正计划通过将图像旋转较小矩形的角度来对图像进行水平调整。旋转时图像处于关闭状态,并且水平或水平调整不完全一两倍。如果在“画图”中打开rotated.bmp,则可以看到顶部矩形不水平。如果有人可以提供帮助,我将不胜感激。我是opencv的新手。这是我的图片和代码。

enter image description here

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

Mat rotated;

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
bool StraightenImage( char* chName );

int main()
{
    StraightenImage("rawimage.bmp");

    waitKey(0);
    return 0;
}



bool StraightenImage( char* chName )
{
    /// Load source image and convert it to gray
    src = imread( chName, 1 );

  const char* source_window = "Source Image";
  namedWindow( source_window, WINDOW_AUTOSIZE );
  imshow( source_window, src );

  /// Convert image to gray and blur it
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );


  bool bRotated = true;
  /// Detect edges using Threshold
  threshold( src_gray, threshold_output, 48, 255, THRESH_BINARY );
  imwrite("threshoutput.bmp", threshold_output);
  /// Find contours
  findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

  /// Find the rotated rectangles and ellipses for each contour
  vector<RotatedRect> minRect( contours.size() );

 size_t i;
 for( i = 0; i < contours.size(); i++ )
 { 
      minRect[i] = minAreaRect( Mat(contours[i]) );

      // find smaller rectangle
      if(minRect[i].size.width > 69 && minRect[i].size.width < 80 && minRect[i].size.height > 390 && minRect[i].size.height < 415 )
      {
          bRotated = false;
          break;
      }
      if(minRect[i].size.height > 69 && minRect[i].size.height < 80 && minRect[i].size.width > 390 && minRect[i].size.width < 415 )
      {
          bRotated = true;
          break;
      }
 }

 if(i == contours.size())
     return false;  // nothing found

// rect is the RotatedRect (I got it from a contour...)
//RotatedRect rect;
// matrices we'll use
Mat M, cropped;
// get angle of smaller rectangle
double angle = minRect[i].angle;

// set size back to src image size 1280x800
minRect[i].size.width = 1280;
minRect[i].size.height = 800;
minRect[i].center.x = 1280/2;
minRect[i].center.y = 800/2;

Size rect_size = minRect[i].size;

if (angle < -45.) {
    angle += 90.0;

    minRect[i].size.width = 800;
    minRect[i].size.height = 1280;
    minRect[i].center.x = 800/2;
    minRect[i].center.y = 1280/2;

    swap(minRect[i].size.width, minRect[i].size.height);
}

// get the rotation matrix
M = getRotationMatrix2D(minRect[i].center, angle, 1.0);
// perform the affine transformation on whole image 1280x800
warpAffine(src, rotated, M, src.size(), INTER_LINEAR);

// save file to disk
imwrite("rotated.bmp", rotated);

/// Create Window 224, 206
const char* rotated_window = "Rotated Image";
namedWindow( rotated_window, WINDOW_AUTOSIZE );
imshow( rotated_window, rotated );

return true;
}

0 个答案:

没有答案