目标:
(1)在一幅二值图提取目标,要求目标外围有一个像素宽的背景像素。如:
。该图是20×40。外围有一个像素的宽度的背景像素(值为255)。为后面描述方便,将该图以3.bmp命名。通过matlab分析得到的像素值如下:
。
(2)在opencv用rectangle函数画矩形,当参数为一个像素宽时,颜色值假设为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均可。读者仔细考虑。