RotatedRect中的非零像素数

RotatedRect中的非零像素数

本文介绍了cv :: RotatedRect中的非零像素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为标题说我想在cv :: Mat的某个区域,即在RotatedRect中找到非零像素的数量。



对于一个常规的Rect,可以简单地在ROI上使用countNonZeroPixels。但是ROI只能是常规(非旋转)矩形。



另一个想法是绘制旋转后的矩形并将其用作掩码。但是openCV不支持旋转矩形的绘制,也不支持countNonZeroPixels接受一个掩码。



有没有人有解决方案如何优雅地解决这个问题?



谢谢!

解决方案

好吧,

这个想法是将图像旋转到矩形的旋转方向,然后在拉直的矩形上应用roi。




  • 如果旋转的矩形不完全在图像内,则会破坏

  • 你可以在旋转之前应用另一个roi来加快速度,整个图像...

      #include< highgui.h> 
    #include< cv.h>


    //从http://stackoverflow.com/questions/2289690/opencv-how-to-rotate-iplimage
    cv :: Mat rotateImage(const cv ::
    {
    cv :: Mat rot_mat = cv :: getRotationMatrix2D(center,angle,1.0);
    cv :: Mat dst;
    cv :: warpAffine(source,dst,rot_mat,source.size());
    return dst;
    }

    int main()
    {
    cv :: namedWindow(test1);

    //我们旋转的矩形
    int x = 300;
    int y = 350;
    int w = 200;
    int h = 50;
    float angle = 47;
    cv :: RotatedRect rect = cv :: RotatedRect(cv :: Point2f(x,y),cv :: Size2f(w,h),angle);

    //空白图像
    cv :: Mat img = cv :: Mat(cv :: Size(640,480),CV_8UC3);

    //绘制旋转的矩形作为椭圆以获得一些视觉反馈
    cv :: ellipse(img,rect,cv :: Scalar(255,0,0),-1);

    //通过rect.angle旋转图像* -1
    cv :: Mat rotimg = rotateImage(img,rect.center,-1 * rect.angle);

    //将roi设置为当前未旋转的矩形
    cv :: Rect roi;
    roi.x = rect.center.x - (rect.size.width / 2);
    roi.y = rect.center.y - (rect.size.height / 2);
    roi.width = rect.size.width;
    roi.height = rect.size.height;

    cv :: imshow(test1,rotimg(roi));
    cv :: waitKey(0);
    }



as the title says i'm trying to find the number of non-zero pixels in a certain area of a cv::Mat, namely within a RotatedRect.

For a regular Rect one could simply use countNonZeroPixels on a ROI. However ROIs can only be regular (non rotated) rectangles.

Another idea was to draw the rotated rectangle and use that as a mask. However openCV neither supports the drawing of rotated rectangles nor does countNonZeroPixels accept a mask.

Does anyone have a solution for how to elegantly solve this ?

Thank you !

解决方案

Ok, so here's my first take at it.

The idea is to rotate the image reverse to the rectangle's rotation and than apply a roi on the straightened rectangle.

  • This will break if the rotated rectangle is not completely within the image
  • You can probably speed this up by applying another roi before rotation to avoid having to rotate the whole image...

    #include <highgui.h>
    #include <cv.h>
    
    
    // From http://stackoverflow.com/questions/2289690/opencv-how-to-rotate-iplimage
    cv::Mat rotateImage(const cv::Mat& source, cv::Point2f center, double angle)
    {
      cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0);
      cv::Mat dst;
      cv::warpAffine(source, dst, rot_mat, source.size());
      return dst;
    }
    
    int main()
    {
      cv::namedWindow("test1");
    
      // Our rotated rect
      int x = 300;
      int y = 350;
      int w = 200;
      int h = 50;
      float angle = 47;
      cv::RotatedRect rect = cv::RotatedRect(cv::Point2f(x,y), cv::Size2f(w,h), angle);
    
      // An empty image
      cv::Mat img = cv::Mat(cv::Size(640, 480), CV_8UC3);
    
      // Draw rotated rect as an ellipse to get some visual feedback
      cv::ellipse(img, rect, cv::Scalar(255,0,0), -1);
    
      // Rotate the image by rect.angle * -1
      cv::Mat rotimg = rotateImage(img, rect.center, -1 * rect.angle);
    
      // Set roi to the now unrotated rectangle
      cv::Rect roi;
      roi.x = rect.center.x - (rect.size.width / 2);
      roi.y = rect.center.y - (rect.size.height / 2);
      roi.width = rect.size.width;
      roi.height = rect.size.height;
    
      cv::imshow("test1", rotimg(roi));
      cv::waitKey(0);
    }
    

这篇关于cv :: RotatedRect中的非零像素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:21