opencv外接矩形和矩阵提取的细节探讨

目标:

(1)在一幅二值图提取目标,要求目标外围有一个像素宽的背景像素。如:

 。该图是20×40。外围有一个像素的宽度的背景像素(值为255)。为后面描述方便,将该图以3.bmp命名。通过matlab分析得到的像素值如下:

 

(2)opencvrectangle函数画矩形,当参数为一个像素宽时,颜色值假设为128,要求线条刚好在最外层。

 

 

 

#include "stdafx.h"
#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,src_gray2;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load source image and convert it to gray
  src_gray = imread( "D:\\U盘备份2012-3-28\\可移动磁盘 (H)\\SZZMJINGJIAN\\pic\\gdsb\\character1.bmp", 0 );
  threshold( src_gray, src_gray, 128, 255, THRESH_BINARY_INV );
  imshow( "TUXIANG", src_gray );
  waitKey(0);


 Mat threshold_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  /// Detect edges using Threshold
  //threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY_INV );
  /// Find contours
  findContours( src_gray2=src_gray.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0) );
   imshow( "TUXIANG2", src_gray );
  /// Approximate contours to polygons + get bounding rects and circles
  vector<vector<Point> > contours_poly( contours.size() );
  vector<Rect> boundRect( contours.size() );
  vector<Point2f>center( contours.size() );
  vector<float>radius( contours.size() );

  for( int i = 0; i < contours.size(); i++ )
     {
   //approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
   boundRect[i] = boundingRect(contours[i]);
   Mat src_gray3=src_gray(boundRect[i]);
   imwrite("D:\\U盘备份2012-3-28\\可移动磁盘 (H)\\SZZMJINGJIAN\\pic\\gdsb\\opencvcharacter1cut1.bmp",src_gray3);
       boundRect[i] = Rect(Point(boundingRect(contours[i]).x-1,boundingRect(contours[i]).y-1),Point(boundingRect(contours[i]).br().x+1,boundingRect(contours[i]).br().y+1));
    Mat src_gray4=src_gray(boundRect[i]);
       imwrite("D:\\U盘备份2012-3-28\\可移动磁盘 (H)\\SZZMJINGJIAN\\pic\\gdsb\\opencvcharacter1cut2.bmp",src_gray4);
  Rect temprect=boundingRect(contours[i]);
  temprect.x-=1;
  temprect.y-=1;
  temprect.width+=2;
  temprect.height+=2;
   Mat src_gray5=src_gray(temprect);
       imwrite("D:\\U盘备份2012-3-28\\可移动磁盘 (H)\\SZZMJINGJIAN\\pic\\gdsb\\opencvcharacter1cut3.bmp",src_gray5);
//      minEnclosingCircle( contours_poly[i], center[i], radius[i] );
     }


  /// Draw polygonal contour + bonding rects + circles
//  Mat drawing = Mat::zeros( src_gray.size(), CV_8UC1 );
  for( int i = 0; i< contours.size(); i++ )
    {
  //     Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
 //    drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
      rectangle( src_gray, boundRect[i].tl(), boundRect[i].br(), 200, 1, 8, 0 );
   imwrite("D:\\U盘备份2012-3-28\\可移动磁盘 (H)\\SZZMJINGJIAN\\pic\\gdsb\\opencvcharacter1.bmp",src_gray);
  //     circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
     }

  /// Show in a window
  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
  imshow( "Contours", src_gray );

  waitKey(0);
  return(0);
}

 结论:

方法1不可以,刚好把外边切除了。

方法2,3均可。读者仔细考虑。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值