
{
cvNamedWindow("Demo")//声燃辩明窗口兄纤
CvCapture* capture=cvCreateCameraCapture(0)//声明摄像头类型的变量
IplImage* frame//声明图像
while(1)//循环
{
frame=cvQueryFrame(capture)//从摄像头中读取一帧
if(!frame) break//如果错误,跳出循环
cvShowImage("Demo",frame)//在窗口中显示图像
char c=cvWaitKey(50)//等待按键事件
if(c==27) break//如果按ESC键,跳出循环
}
cvReleaseCapture(&capture)//释放capture
cvDestroyWindow("Demo")//销掉窗口
}
采用金字塔方法进行图像分割#include "cv.h"
#include "highgui.h"
#include <math.h>
IplImage* image[2] = { 0, 0 }, *image0 = 0, *image1 = 0
CvSize size
int w0, h0,i
int threshold1, threshold2
int l,level = 4
int sthreshold1, sthreshold2
int l_comp
int block_size = 1000
float parameter
double threshold
double rezult, min_rezult
CvFilter filter = CV_GAUSSIAN_5x5
CvConnectedComp *cur_comp, min_comp
CvSeq *comp
CvMemStorage *storage
CvPoint pt1, pt2
void ON_SEGMENT(int a)
{
cvPyrSegmentation(image0, image1, storage, &comp,
level, threshold1+1, threshold2+1)
/*l_comp = comp->total
i = 0
min_comp.value = cvScalarAll(0)
while(i<l_comp)
{
cur_comp = (CvConnectedComp*)cvGetSeqElem ( comp, i )
if(fabs(255- min_comp.value.val[0])>
fabs(255- cur_comp->value.val[0]) &&
fabs(min_comp.value.val[1])>
fabs(cur_comp->value.val[1]) &&
fabs(min_comp.value.val[2])>
fabs(cur_comp->薯纯尺value.val[2]) )
min_comp = *cur_comp
i++
}*/
cvShowImage("Segmentation", image1)
}
int main( int argc, char** argv )
{
char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg"
if( (image[0] = cvLoadImage( filename, 1)) == 0 )
return -1
cvNamedWindow("Source", 0)
cvShowImage("Source", image[0])
cvNamedWindow("Segmentation", 0)
storage = cvCreateMemStorage ( block_size )
image[0]->width &= -(1<<level)
image[0]->height &= -(1<<level)
image0 = cvCloneImage( image[0] )
image1 = cvCloneImage( image[0] )
/数高/ 对彩色图像进行分割
l = 1
threshold1 =255
threshold2 =30
ON_SEGMENT(1)
sthreshold1 = cvCreateTrackbar("Threshold1"裤缓, "Segmentation", &threshold1, 255,
ON_SEGMENT)
sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation", &threshold2, 255,
ON_SEGMENT)
cvShowImage("Segmentation", image1)
cvWaitKey(0)
cvDestroyWindow("Segmentation")
cvDestroyWindow("Source")
cvReleaseMemStorage(&storage )
cvReleaseImage(&image[0])
cvReleaseImage(&image0)
cvReleaseImage(&image1)
return 0
}
试试 里面一些参数要改一下 这是c++vector<vector<Point>>vecContour
vector<Vec4i>hierarcy
findContours(dst1, vecContour, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE)//查找轮廓
RotatedRect rorect//存放最小外接矩形
Point2f rect[4]
//vector<vector<Point>>proRect
//Mat imageContours = Mat::zeros(image.size(), CV_8UC1)
for (int i = 0i <友瞎 vecContour.size()i++)
{
boundRect = boundingRect(Mat(vecContour[i]))
if (boundRect.width <200 || boundRect.height <200)
continue
circle(dst1, Point(rorect.center.x, rorect.center.y), 5, Scalar(0, 255, 0), -1, 8) /橡袭/绘制最小外接矩形的中心点
rorect.points(rect) //把最梁告兄小外接矩形四个端点复制给rect数组
rectangle(dst1, Point(boundRect.x, boundRect.y), Point(boundRect.x + boundRect.width, boundRect.y + boundRect.height), Scalar(0, 255, 0), 2, 8)
for (int j = 0j <4j++)
{
line(dst1, rect[j], rect[(j + 1) % 4], Scalar(255, 0, 0), 2, 8) //绘制最小外接矩形每条边
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)