opencv如何在标识颜色区域画矩形
我需要的物体目标已经用特定的颜色替换了,不如说红色,那接下来我如何用矩形把最大的红色区域框选出来...
我需要的物体目标已经用特定的颜色替换了,不如说红色,那接下来我如何用矩形把最大的红色区域框选出来
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏20(财富值+成长值)
展开全部
给你个参考,可以改一改
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
CvRect g_rect;
bool g_isdown;
void my_callback(int event, int x, int y, int flags, void* param);
//将image图像的rect区域部分做加亮处理
void high_light(IplImage *image,CvRect rect);
//对选中的部分绘制直方图
void Draw_hist(IplImage* img,CvRect Rect);
int main()
{
IplImage *orginal_image=cvLoadImage("fruits.jpg");
assert(orginal_image!=NULL);
IplImage *temp_image=cvCreateImage(cvGetSize(orginal_image),
orginal_image->depth,
orginal_image->nChannels);
assert(temp_image!=NULL);
cvNamedWindow("Show");
cvSetMouseCallback("Show",my_callback,(void*)temp_image);
while (1)
{IplImage *temp_image= cvCloneImage(orginal_image);
if (true==g_isdown)
{
high_light(temp_image,g_rect);
Draw_hist(temp_image,g_rect);
}
else
{
high_light(temp_image,g_rect);
Draw_hist(temp_image,g_rect);
}
cvShowImage("Show",temp_image);
if (27==cvWaitKey(30))
{
break;
}
}
cvDestroyAllWindows();
return 0;
}
void my_callback(int event, int x, int y, int flags, void* param)
{
IplImage *image=(IplImage*)(param);
switch (event)
{
case CV_EVENT_LBUTTONDOWN :{
g_isdown=true;
g_rect=cvRect(x,y,0,0);
}
break;
case CV_EVENT_MOUSEMOVE:{
if (true==g_isdown)
{
g_rect.width=x-g_rect.x;
g_rect.height=y-g_rect.y;
}
}
break;
case CV_EVENT_LBUTTONUP :{
g_isdown=false;
if (g_rect.width<0)
{
g_rect.x=g_rect.x+g_rect.width;
g_rect.width*=-1;
}
if (g_rect.height<0)
{
g_rect.y=g_rect.y+g_rect.height;
g_rect.height*=-1;
}
high_light(image,g_rect);
Draw_hist(image,g_rect);
}
break;
}
}
//将图像进行加亮处理
void high_light(IplImage *image,CvRect rect)
{
assert(image!=NULL);
int row,col;
for (row=rect.y;row<rect.y+rect.height;row++)
{
uchar *ptr=(uchar*)(image->imageData+row*image->widthStep);
for (col=rect.x;col<rect.x+rect.width;col++)
{
ptr[(col*3+1)]=150;
ptr[(col*3+2)]=110;//
}
if (row>=image->height-1)
{
row=image->height;
}
}
}
//对选中的区域绘制直方图
void Draw_hist(IplImage* img,CvRect Rect)
{
if (Rect.x<=0||Rect.y<=0||Rect.width<=0||Rect.height<=0)
{
return;
}
cvSetImageROI(img,Rect);
IplImage* hsv = cvCreateImage(/*cvSize(Rect.width ,Rect.height)*/cvGetSize(img), 8, 3 );
IplImage* h_plane = cvCreateImage(/*cvSize(Rect.width ,Rect.height)*/cvGetSize(img), 8, 1 );
IplImage* s_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );
IplImage* v_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
int h_bins = 16, s_bins = 8;
int hist_size[] = {h_bins, s_bins};
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
cvCvtColor( img, hsv, CV_BGR2HSV );
cvResetImageROI(img);
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
cvCalcHist( planes, hist, 0, 0 );
float max_value;
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
int height = 480;
int width = (h_bins*s_bins*6);
IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );
cvZero( hist_img );
IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);
IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);
int bin_w = width / (h_bins * s_bins);
for(int h = 0; h < h_bins; h++)
{
for(int s = 0; s < s_bins; s++)
{
int i = h*s_bins + s;
/** 获得直方图中的统计次数,计算显示在图像中的高度 */
float bin_val = cvQueryHistValue_2D( hist, h, s );
int intensity = cvRound(bin_val*height/max_value);
/** 获得当前直方图代表的颜色,转换成RGB用于绘制 */
cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0));
cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);
CvScalar color = cvGet2D(rgb_color,0,0);
cvRectangle( hist_img, cvPoint(i*bin_w,height),
cvPoint((i+1)*bin_w,height - intensity),
color, -1, 8, 0 );
}
}
cvNamedWindow( "H-S Histogram", 1 );
cvShowImage( "H-S Histogram", hist_img );
}
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
CvRect g_rect;
bool g_isdown;
void my_callback(int event, int x, int y, int flags, void* param);
//将image图像的rect区域部分做加亮处理
void high_light(IplImage *image,CvRect rect);
//对选中的部分绘制直方图
void Draw_hist(IplImage* img,CvRect Rect);
int main()
{
IplImage *orginal_image=cvLoadImage("fruits.jpg");
assert(orginal_image!=NULL);
IplImage *temp_image=cvCreateImage(cvGetSize(orginal_image),
orginal_image->depth,
orginal_image->nChannels);
assert(temp_image!=NULL);
cvNamedWindow("Show");
cvSetMouseCallback("Show",my_callback,(void*)temp_image);
while (1)
{IplImage *temp_image= cvCloneImage(orginal_image);
if (true==g_isdown)
{
high_light(temp_image,g_rect);
Draw_hist(temp_image,g_rect);
}
else
{
high_light(temp_image,g_rect);
Draw_hist(temp_image,g_rect);
}
cvShowImage("Show",temp_image);
if (27==cvWaitKey(30))
{
break;
}
}
cvDestroyAllWindows();
return 0;
}
void my_callback(int event, int x, int y, int flags, void* param)
{
IplImage *image=(IplImage*)(param);
switch (event)
{
case CV_EVENT_LBUTTONDOWN :{
g_isdown=true;
g_rect=cvRect(x,y,0,0);
}
break;
case CV_EVENT_MOUSEMOVE:{
if (true==g_isdown)
{
g_rect.width=x-g_rect.x;
g_rect.height=y-g_rect.y;
}
}
break;
case CV_EVENT_LBUTTONUP :{
g_isdown=false;
if (g_rect.width<0)
{
g_rect.x=g_rect.x+g_rect.width;
g_rect.width*=-1;
}
if (g_rect.height<0)
{
g_rect.y=g_rect.y+g_rect.height;
g_rect.height*=-1;
}
high_light(image,g_rect);
Draw_hist(image,g_rect);
}
break;
}
}
//将图像进行加亮处理
void high_light(IplImage *image,CvRect rect)
{
assert(image!=NULL);
int row,col;
for (row=rect.y;row<rect.y+rect.height;row++)
{
uchar *ptr=(uchar*)(image->imageData+row*image->widthStep);
for (col=rect.x;col<rect.x+rect.width;col++)
{
ptr[(col*3+1)]=150;
ptr[(col*3+2)]=110;//
}
if (row>=image->height-1)
{
row=image->height;
}
}
}
//对选中的区域绘制直方图
void Draw_hist(IplImage* img,CvRect Rect)
{
if (Rect.x<=0||Rect.y<=0||Rect.width<=0||Rect.height<=0)
{
return;
}
cvSetImageROI(img,Rect);
IplImage* hsv = cvCreateImage(/*cvSize(Rect.width ,Rect.height)*/cvGetSize(img), 8, 3 );
IplImage* h_plane = cvCreateImage(/*cvSize(Rect.width ,Rect.height)*/cvGetSize(img), 8, 1 );
IplImage* s_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );
IplImage* v_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
int h_bins = 16, s_bins = 8;
int hist_size[] = {h_bins, s_bins};
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
cvCvtColor( img, hsv, CV_BGR2HSV );
cvResetImageROI(img);
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
cvCalcHist( planes, hist, 0, 0 );
float max_value;
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
int height = 480;
int width = (h_bins*s_bins*6);
IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );
cvZero( hist_img );
IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);
IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);
int bin_w = width / (h_bins * s_bins);
for(int h = 0; h < h_bins; h++)
{
for(int s = 0; s < s_bins; s++)
{
int i = h*s_bins + s;
/** 获得直方图中的统计次数,计算显示在图像中的高度 */
float bin_val = cvQueryHistValue_2D( hist, h, s );
int intensity = cvRound(bin_val*height/max_value);
/** 获得当前直方图代表的颜色,转换成RGB用于绘制 */
cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0));
cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);
CvScalar color = cvGet2D(rgb_color,0,0);
cvRectangle( hist_img, cvPoint(i*bin_w,height),
cvPoint((i+1)*bin_w,height - intensity),
color, -1, 8, 0 );
}
}
cvNamedWindow( "H-S Histogram", 1 );
cvShowImage( "H-S Histogram", hist_img );
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询