资源简介
用opencv识别魔方图片颜色,完成魔方初始顺序输入,这只是opencv识别一张魔方图片demo
代码片段和文件信息
// cube.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
CvScalar getAvgBGR(IplImage *img)
{
int width = img->width;
int height = img->height;
double b=0 g=0 r=0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
CvScalar s = cvGet2D(img j i);
b += s.val[0];
g += s.val[1];
r += s.val[2];
}
}
b = b / (width*height);
g = g / (width*height);
r = r / (width*height);
CvScalar scalar = cvScalar(b g r);
return scalar;
}
void printColor(CvScalar scalar)
{
double b = scalar.val[0];
double g = scalar.val[1];
double r = scalar.val[2];
if (b > 100 && g < 50 && r < 50)
{
printf(“蓝 “);
}
else if (b>100 && g>100 && r > 100)
{
printf(“白 “);
}
else if (b < 50 && g < 50 && r > 50)
{
printf(“红 “);
}
else if (b < 50 && g >100 && r > 100)
{
printf(“黄 “);
}
else if (b < 70 && g > 50 && r < 50)
{
printf(“绿 “);
}
}
int _tmain(int argc _TCHAR* argv[])
{
IplImage* img = cvLoadImage(“cube.jpg“);
IplImage *gray = cvCreateImage(cvGetSize(img) 8 1);
cvCvtColor(img gray CV_BGR2GRAY);
IplImage *dst_median = cvCreateImage(cvGetSize(img) 8 1);
cvSmooth(gray dst_median CV_MEDIAN 7 7 0 0);
cvThreshold(dst_median dst_median 100 255 CV_THRESH_BINARY);
cvShowImage(“thres“ dst_median);
CvSeq *pContour = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
CvBox2D s;
cvFindContours(dst_median storage &pContour sizeof(CvContour) CV_RETR_EXTERNAL CV_CHAIN_APPROX_SIMPLE);
CvRect rect;
for (CvSeq* c = pContour; c != NULL; c = c->h_next)
{
rect = cvBoundingRect(pContour 0);
cvRectangle(img cvPoint(rect.x rect.y) cvPoint(rect.x + rect.width rect.y + rect.height)cvScalar(0 0 255) 2);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
CvRect tmp = cvRect(rect.x + j*(rect.width / 3) rect.y + i*(rect.height / 3) rect.width / 3 rect.height / 3);
CvRect center = cvRect(tmp.x + tmp.width / 2 - 20 tmp.y + tmp.height / 2 - 20 40 40);
cvSetImageROI(img center);
IplImage *roi = cvCreateImage(cvSize(center.widthcenter.height)83);
cvCopy(img roi);
CvScalar scalar = getAvgBGR(roi);
//printf(“avg:%lf%lf%lf\n“ scalar.val[0] scalar.val[1] scalar.val[2]);
printColor(scalar);
cvReleaseImage(&roi);
cvResetImageROI(img);
cvRectangle(img cvPoint(tmp.x tmp.y) cvPoint(tmp.x + tmp.width tmp.y + tmp.height) cvScalar(0 0 255) 2);
cvRectangle(img cvPoint(center.x center.y) cvPoint(center.x + center.width center.y + center.height) cvScalar(255 0 255) 2);
}
printf(“\n-----------\n“);
}
cvShowImage(“threshold“ dst_median);
cvShowImage(“src“ img);
cvWaitKey(0);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2861 2018-06-05 22:13 cube-lesson05\cube\cube.cpp
文件 244727 2018-06-05 20:27 cube-lesson05\cube\cube.jpg
文件 5362 2018-06-05 20:40 cube-lesson05\cube\cube.vcxproj
文件 1311 2018-05-25 21:35 cube-lesson05\cube\cube.vcxproj.filters
文件 1489 2018-05-25 21:35 cube-lesson05\cube\ReadMe.txt
文件 210 2018-05-25 21:35 cube-lesson05\cube\stdafx.cpp
文件 234 2018-05-25 21:35 cube-lesson05\cube\stdafx.h
文件 236 2018-05-25 21:35 cube-lesson05\cube\targetver.h
文件 958 2018-05-25 21:35 cube-lesson05\cube.sln
..A..H. 17408 2018-05-25 21:43 cube-lesson05\cube.v12.suo
目录 0 2018-06-05 22:15 cube-lesson05\cube
目录 0 2018-06-05 22:15 cube-lesson05
----------- --------- ---------- ----- ----
274796 12
- 上一篇:Qt魔方还原lesson4-魔方完全还原
- 下一篇:用PFC画颗粒级配曲线
评论
共有 条评论