#include "highgui.h"
#include "cv.h"
#include "cxcore.h"
#include "cvaux.h"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <windows.h>
using namespace std;
using namespace cv;
//color
int errorx = 0;
#define RED_H_LOW 0
#define RED_H_HIGH 7
#define RED_S_LOW 240
#define RED_S_HIGH 255
int squre = 10;
int top_red = 0;
int low_red = 255;
int top_green = 0;
int low_green = 255;
int top_blue = 0;
int low_blue = 255;
int smallcirle = 20;
int top_h = 0;
int low_h = 180;
int top_s = 0;
int low_s = 255;
int top_v = 0;
int low_v = 255;
bool FindColorScope = 0;
/*面临的问题 如何将绘制的曲线保存?*/
struct GFPoint {
int x = 0;
int y = 0;
int ok = 0;
}GFpoint[9];
GFPoint first_point, last_point;
//frame2寻找识别物体的像素点的圆心
int a[10];
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <windows.h>
using namespace std;
using namespace cv;
//color
int errorx = 0;
#define RED_H_LOW 0
#define RED_H_HIGH 7
#define RED_S_LOW 240
#define RED_S_HIGH 255
int squre = 10;
int top_red = 0;
int low_red = 255;
int top_green = 0;
int low_green = 255;
int top_blue = 0;
int low_blue = 255;
int smallcirle = 20;
int top_h = 0;
int low_h = 180;
int top_s = 0;
int low_s = 255;
int top_v = 0;
int low_v = 255;
bool FindColorScope = 0;
/*面临的问题 如何将绘制的曲线保存?*/
struct GFPoint {
int x = 0;
int y = 0;
int ok = 0;
}GFpoint[9];
GFPoint first_point, last_point;
//frame2寻找识别物体的像素点的圆心
int a[10];
int num = 0;
void GFFindPoint(IplImage* frame, IplImage *img)
{
int x, y;
int frame_x = 0, frame_y = 0, num = 0;
for (x = 0; x < frame->width; x++) {
for (y = 0; y < frame->height; y++) {
void GFFindPoint(IplImage* frame, IplImage *img)
{
int x, y;
int frame_x = 0, frame_y = 0, num = 0;
for (x = 0; x < frame->width; x++) {
for (y = 0; y < frame->height; y++) {
// If its a tracked pixel, count it to the center of gravity's calcul
if (((uchar *)(frame->imageData + y*frame->widthStep))[x] == 255) {
frame_x += x;
frame_y += y;
num++;
}
}
}
if (((uchar *)(frame->imageData + y*frame->widthStep))[x] == 255) {
frame_x += x;
frame_y += y;
num++;
}
}
}
if (num == 0)
return;
last_point.x = frame_x / num;
last_point.y = frame_y / num;
//cout << "x= " << last_point.x << " y= " << last_point.y << endl;
Point center = Point(last_point.x, last_point.y);
cvCircle(img, center, smallcirle / 2, Scalar(0, 0, 255), smallcirle / 2);
return;
last_point.x = frame_x / num;
last_point.y = frame_y / num;
//cout << "x= " << last_point.x << " y= " << last_point.y << endl;
Point center = Point(last_point.x, last_point.y);
cvCircle(img, center, smallcirle / 2, Scalar(0, 0, 255), smallcirle / 2);
}
//将两点连接起来。
void GFDrawLine(IplImage* frame, IplImage* frame1)
{
Point centerf = Point(first_point.x, first_point.y);
Point centerl = Point(last_point.x, last_point.y);
for (int i = 0; i < 9; i++)
{
//将两点连接起来。
void GFDrawLine(IplImage* frame, IplImage* frame1)
{
Point centerf = Point(first_point.x, first_point.y);
Point centerl = Point(last_point.x, last_point.y);
for (int i = 0; i < 9; i++)
{
if (GFpoint[i].x <= smallcirle / 2 + last_point.x&&
GFpoint[i].x >= last_point.x - smallcirle / 2 &&
GFpoint[i].y <= smallcirle / 2 + last_point.y&&
GFpoint[i].y >= last_point.y - smallcirle / 2 &&
GFpoint[i].ok == 0
)
{
GFpoint[i].ok++;
Point cl = Point(GFpoint[i].x, GFpoint[i].y);
cvLine(frame1, centerf, centerl, Scalar(0, 0, 255), 10);
first_point.x = last_point.x;
first_point.y = last_point.y;
a[num++]=i;
//cout << i;
break;
}
}
GFpoint[i].x >= last_point.x - smallcirle / 2 &&
GFpoint[i].y <= smallcirle / 2 + last_point.y&&
GFpoint[i].y >= last_point.y - smallcirle / 2 &&
GFpoint[i].ok == 0
)
{
GFpoint[i].ok++;
Point cl = Point(GFpoint[i].x, GFpoint[i].y);
cvLine(frame1, centerf, centerl, Scalar(0, 0, 255), 10);
first_point.x = last_point.x;
first_point.y = last_point.y;
a[num++]=i;
//cout << i;
break;
}
}
cvLine(frame, centerf, centerl, Scalar(0, 0, 255), 10);
}
//将frame与frame1叠加
void GFAddFrame(IplImage* frame, IplImage* frame1)
{
for (int y = 0; y < frame1->height; y++)
{
uchar *prt = (uchar*)(frame1->imageData + y*frame1->widthStep);
uchar *fprt = (uchar*)(frame->imageData + y*frame->widthStep);
for (int x = 0; x < frame1->width; x++)
{
if (prt[3 * x] == 255 &&
prt[3 * x + 1] == 255 &&//s 饱和度
prt[3 * x + 2] == 255)//v 明度
{
continue;
}
else
{
fprt[3 * x] = prt[3 * x];
fprt[3 * x + 1] = prt[3 * x + 1];
fprt[3 * x + 2] = prt[3 * x + 2];
}
}
}
}
//寻找到第一个点
void GFFindFistPoint(IplImage* frame)
{
for (int i = 0; i < 9; i++)
{
if (GFpoint[i].x <= smallcirle / 2 + last_point.x&&
GFpoint[i].x >= last_point.x - smallcirle / 2 &&
GFpoint[i].y <= smallcirle / 2 + last_point.y&&
GFpoint[i].y >= last_point.y - smallcirle / 2
)
{
//Point center = Point(GFpoint[i].x, GFpoint[i].y);
//cvCircle(frame, center, 5, Scalar(0, 0, 255), 5);
first_point.ok = 1;
first_point.x = GFpoint[i].x;
first_point.y = GFpoint[i].y;
a[num++]=i;
//cout << i;
break;
}
}
}
void GFDrawCircle(IplImage* frame)
{
for (int i = 0; i < 9; i++)
{
//将frame与frame1叠加
void GFAddFrame(IplImage* frame, IplImage* frame1)
{
for (int y = 0; y < frame1->height; y++)
{
uchar *prt = (uchar*)(frame1->imageData + y*frame1->widthStep);
uchar *fprt = (uchar*)(frame->imageData + y*frame->widthStep);
for (int x = 0; x < frame1->width; x++)
{
if (prt[3 * x] == 255 &&
prt[3 * x + 1] == 255 &&//s 饱和度
prt[3 * x + 2] == 255)//v 明度
{
continue;
}
else
{
fprt[3 * x] = prt[3 * x];
fprt[3 * x + 1] = prt[3 * x + 1];
fprt[3 * x + 2] = prt[3 * x + 2];
}
}
}
}
//寻找到第一个点
void GFFindFistPoint(IplImage* frame)
{
for (int i = 0; i < 9; i++)
{
if (GFpoint[i].x <= smallcirle / 2 + last_point.x&&
GFpoint[i].x >= last_point.x - smallcirle / 2 &&
GFpoint[i].y <= smallcirle / 2 + last_point.y&&
GFpoint[i].y >= last_point.y - smallcirle / 2
)
{
//Point center = Point(GFpoint[i].x, GFpoint[i].y);
//cvCircle(frame, center, 5, Scalar(0, 0, 255), 5);
first_point.ok = 1;
first_point.x = GFpoint[i].x;
first_point.y = GFpoint[i].y;
a[num++]=i;
//cout << i;
break;
}
}
}
void GFDrawCircle(IplImage* frame)
{
for (int i = 0; i < 9; i++)
{
Point center = Point(GFpoint[i].x, GFpoint[i].y);
cvCircle(frame, center, frame->height / 6, Scalar(255, 0, 0), 2);
}
}
void FillWhite(IplImage* frame)
{
for (int y = 0; y < frame->height; y++)
{
uchar *prt = (uchar*)(frame->imageData + y*frame->widthStep)
;
for (int x = 0; x < frame->width; x++)
{
prt[3 * x] = 255;//h 色调
prt[3 * x + 1] = 255;//s 饱和度
prt[3 * x + 2] = 255;//v 明度
}
}
}
void GFFindCirclePoint(IplImage* frame)
{
int num = 0;
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
int r = frame->height / 6;
//Point center = Point(r * i + r*(i - 1), r * j + r*(j - 1));
GFpoint[num].x = r * i + r*(i - 1);
GFpoint[num++].y = r * j + r*(j - 1);
//cvCircle(frame, center, r, Scalar(255, 0, 0), 5);
}
}
}
void GFFindColorPoint(IplImage* img, int lowx, int topx,int lowy,int topy){
for (int y = lowy; y < topy; y++)
{
uchar *prt = (uchar*)(img->imageData + y*img->widthStep);
for (int x = lowx; x < topx; x++)
{
if (top_h<prt[3 * x])
{
top_h = prt[3 * x];
}
if (low_h>prt[3 * x])
{
low_h = prt[3 * x];
}
if (top_s < prt[3 * x + 1])
{
top_s = prt[3 * x + 1];
}
if (low_s>prt[3 * x + 1])
{
low_s = prt[3 * x + 1];
}
if (top_v < prt[3 * x + 2])
{
top_v = prt[3 * x + 2];
}
if (low_v>prt[3 * x + 2])
{
low_v = prt[3 * x + 2];
}
}
void FillWhite(IplImage* frame)
{
for (int y = 0; y < frame->height; y++)
{
uchar *prt = (uchar*)(frame->imageData + y*frame->widthStep)
;
for (int x = 0; x < frame->width; x++)
{
prt[3 * x] = 255;//h 色调
prt[3 * x + 1] = 255;//s 饱和度
prt[3 * x + 2] = 255;//v 明度
}
}
}
void GFFindCirclePoint(IplImage* frame)
{
int num = 0;
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
int r = frame->height / 6;
//Point center = Point(r * i + r*(i - 1), r * j + r*(j - 1));
GFpoint[num].x = r * i + r*(i - 1);
GFpoint[num++].y = r * j + r*(j - 1);
//cvCircle(frame, center, r, Scalar(255, 0, 0), 5);
}
}
}
void GFFindColorPoint(IplImage* img, int lowx, int topx,int lowy,int topy){
for (int y = lowy; y < topy; y++)
{
uchar *prt = (uchar*)(img->imageData + y*img->widthStep);
for (int x = lowx; x < topx; x++)
{
if (top_h<prt[3 * x])
{
top_h = prt[3 * x];
}
if (low_h>prt[3 * x])
{
low_h = prt[3 * x];
}
if (top_s < prt[3 * x + 1])
{
top_s = prt[3 * x + 1];
}
if (low_s>prt[3 * x + 1])
{
low_s = prt[3 * x + 1];
}
if (top_v < prt[3 * x + 2])
{
top_v = prt[3 * x + 2];
}
if (low_v>prt[3 * x + 2])
{
low_v = prt[3 * x + 2];
}
}
}
}
//腐蚀和膨胀
void GFErodeAndDilate(IplImage* img)
{
cvErode(img, img, NULL, 1); //腐蚀
cvDilate(img, img, NULL, 1); //膨胀
}
int mouse_x = -1, mouse_y = -1;
void on_mouse(int event, int x, int y, int flags, void* param)
{
IplImage *findcolor = (IplImage*) param;
switch (event)
//腐蚀和膨胀
void GFErodeAndDilate(IplImage* img)
{
cvErode(img, img, NULL, 1); //腐蚀
cvDilate(img, img, NULL, 1); //膨胀
}
int mouse_x = -1, mouse_y = -1;
void on_mouse(int event, int x, int y, int flags, void* param)
{
IplImage *findcolor = (IplImage*) param;
switch (event)
{
case CV_EVENT_LBUTTONUP:
{
GFFindColorPoint(findcolor, x - squre,x+squre, y-squre,y + squre);
}
break;
case CV_EVENT_LBUTTONDOWN:
{
Point lt = Point(x - squre, y - squre);
Point rl = Point(x + squre, y + squre);
cvRectangle(findcolor, lt, rl, Scalar(0, 255, 0));
}
break;
}
case CV_EVENT_LBUTTONUP:
{
GFFindColorPoint(findcolor, x - squre,x+squre, y-squre,y + squre);
}
break;
case CV_EVENT_LBUTTONDOWN:
{
Point lt = Point(x - squre, y - squre);
Point rl = Point(x + squre, y + squre);
cvRectangle(findcolor, lt, rl, Scalar(0, 255, 0));
}
break;
}
}
void GFFindColorScope()
{
CvCapture * capture;
capture = cvCreateCameraCapture(0);
IplImage * findcolor;
cvNamedWindow("FindColor", CV_WINDOW_AUTOSIZE);
void GFFindColorScope()
{
CvCapture * capture;
capture = cvCreateCameraCapture(0);
IplImage * findcolor;
cvNamedWindow("FindColor", CV_WINDOW_AUTOSIZE);
while (findcolor = cvQueryFrame(capture)){
GFErodeAndDilate(findcolor);
cvFlip(findcolor, findcolor, 1);
cvSmooth(findcolor, findcolor, CV_GAUSSIAN, 5, 5);
cvCvtColor(findcolor, findcolor, CV_BGR2HSV);
if (findcolor->height > findcolor->width)
{
errorx = findcolor->height - findcolor->width;
//findcolor->height = findcolor->width;
}
else
{
errorx = findcolor->width - findcolor->height;
//findcolor->width = findcolor->height;
}
cvSetMouseCallback("FindColor", on_mouse, (void*)findcolor);
//cvCvtColor(findcolor, findcolor, CV_HSV2BGR);
cvShowImage("FindColor", findcolor);
char key = cvWaitKey(33);
if (key == 27)
break;
}
cvFlip(findcolor, findcolor, 1);
cvSmooth(findcolor, findcolor, CV_GAUSSIAN, 5, 5);
cvCvtColor(findcolor, findcolor, CV_BGR2HSV);
if (findcolor->height > findcolor->width)
{
errorx = findcolor->height - findcolor->width;
//findcolor->height = findcolor->width;
}
else
{
errorx = findcolor->width - findcolor->height;
//findcolor->width = findcolor->height;
}
cvSetMouseCallback("FindColor", on_mouse, (void*)findcolor);
//cvCvtColor(findcolor, findcolor, CV_HSV2BGR);
cvShowImage("FindColor", findcolor);
char key = cvWaitKey(33);
if (key == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("FindColor");
/*cout << " top_h =" << top_h << endl;
cout << " low_h = " << low_h << endl;
cout << " top_s = " << top_s << endl;
cout << " low_s = " << low_s << endl;
cout << " top_v = " << top_v << endl;
cout << " low_v = " << low_v << endl;
cout << " errorx = " << errorx << endl;*/
}
void showframe2(IplImage * frame, IplImage * img)
{
cvZero(img);
for (int y = 0; y < frame->height; y++)
{
uchar *prt = (uchar*)(frame->imageData + y*frame->widthStep);
uchar *iprt = (uchar*)(img->imageData + y*img->widthStep);
for (int x = 0; x < frame->width; x++)
{
if (prt[3 * x] < top_h &&prt[3 * x] >= low_h &&
prt[3 * x + 1] < top_s &&prt[3 * x + 1] >= low_s &&
prt[3 * x + 2] < top_v &&prt[3 * x + 2] >= low_v)
cvDestroyWindow("FindColor");
/*cout << " top_h =" << top_h << endl;
cout << " low_h = " << low_h << endl;
cout << " top_s = " << top_s << endl;
cout << " low_s = " << low_s << endl;
cout << " top_v = " << top_v << endl;
cout << " low_v = " << low_v << endl;
cout << " errorx = " << errorx << endl;*/
}
void showframe2(IplImage * frame, IplImage * img)
{
cvZero(img);
for (int y = 0; y < frame->height; y++)
{
uchar *prt = (uchar*)(frame->imageData + y*frame->widthStep);
uchar *iprt = (uchar*)(img->imageData + y*img->widthStep);
for (int x = 0; x < frame->width; x++)
{
if (prt[3 * x] < top_h &&prt[3 * x] >= low_h &&
prt[3 * x + 1] < top_s &&prt[3 * x + 1] >= low_s &&
prt[3 * x + 2] < top_v &&prt[3 * x + 2] >= low_v)
{
iprt[3 * x] = 255;//h 色调
iprt[3 * x + 1] = 255;//s 饱和度
iprt[3 * x + 2] = 255;//v 明度
}
}
}
}
iprt[3 * x] = 255;//h 色调
iprt[3 * x + 1] = 255;//s 饱和度
iprt[3 * x + 2] = 255;//v 明度
}
}
}
}
void main()
{
{
freopen("E:\\ConsoleApplication1\\ConsoleApplication1\\out.txt", "w",stdout);
GFFindColorScope();
memset(a, 0, sizeof(a));
CvCapture * capture;
capture = cvCreateCameraCapture(0);
cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
cvNamedWindow("frame1", CV_WINDOW_AUTOSIZE);
cvNamedWindow("frame2", CV_WINDOW_AUTOSIZE);
IplImage* frame2;
IplImage* frame;
frame = cvQueryFrame(capture);
GFFindColorScope();
memset(a, 0, sizeof(a));
CvCapture * capture;
capture = cvCreateCameraCapture(0);
cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
cvNamedWindow("frame1", CV_WINDOW_AUTOSIZE);
cvNamedWindow("frame2", CV_WINDOW_AUTOSIZE);
IplImage* frame2;
IplImage* frame;
frame = cvQueryFrame(capture);
if (!frame)
return;
if (frame->height > frame->width)
{
errorx = frame->height - frame->width;
//frame->height = frame->width;
}
else
{
errorx = frame->width - frame->height;
//frame->width = frame->height;
}
frame2 = cvCreateImage(cvSize(frame->width, frame->height), 8, 3);
IplImage * frame1 = cvCreateImage(cvSize(frame->width, frame->height), 8, 3);
GFFindCirclePoint(frame);
//..frame1->height = frame1->width = frame->width;
return;
if (frame->height > frame->width)
{
errorx = frame->height - frame->width;
//frame->height = frame->width;
}
else
{
errorx = frame->width - frame->height;
//frame->width = frame->height;
}
frame2 = cvCreateImage(cvSize(frame->width, frame->height), 8, 3);
IplImage * frame1 = cvCreateImage(cvSize(frame->width, frame->height), 8, 3);
GFFindCirclePoint(frame);
//..frame1->height = frame1->width = frame->width;
FillWhite(frame1);
while (1)
{
frame = cvQueryFrame(capture);
cvFlip(frame, frame, 1);
cvSmooth(frame, frame, CV_GAUSSIAN, 5, 5);
GFErodeAndDilate(frame);
cvCvtColor(frame, frame, CV_BGR2HSV);
if (!frame)
break;
//if (frame->height > frame->width)
//frame->height = frame->width;
//else
//frame->width = frame->height;
while (1)
{
frame = cvQueryFrame(capture);
cvFlip(frame, frame, 1);
cvSmooth(frame, frame, CV_GAUSSIAN, 5, 5);
GFErodeAndDilate(frame);
cvCvtColor(frame, frame, CV_BGR2HSV);
if (!frame)
break;
//if (frame->height > frame->width)
//frame->height = frame->width;
//else
//frame->width = frame->height;
//Size size(120, 120);
//cvResize(frame, frame,(120,120));
showframe2(frame, frame2);
GFDrawCircle(frame);
GFFindPoint(frame2, frame);
//cvResize(frame, frame,(120,120));
showframe2(frame, frame2);
GFDrawCircle(frame);
GFFindPoint(frame2, frame);
if (!first_point.ok){
GFFindFistPoint(frame);
}
else
{
GFDrawLine(frame, frame1);
GFAddFrame(frame, frame1);
}
//cvFlip(frame1, frame1, 1);
//cvFlip(frame2, frame2, 1);
// cvCvtColor(frame, frame, CV_HSV2BGR);
cvShowImage("frame", frame);
cvShowImage("frame1", frame1);
cvShowImage("frame2", frame2);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Camera Show");
for (int i = 1; i < 10; i++)
cout << a[i];
}
GFFindFistPoint(frame);
}
else
{
GFDrawLine(frame, frame1);
GFAddFrame(frame, frame1);
}
//cvFlip(frame1, frame1, 1);
//cvFlip(frame2, frame2, 1);
// cvCvtColor(frame, frame, CV_HSV2BGR);
cvShowImage("frame", frame);
cvShowImage("frame1", frame1);
cvShowImage("frame2", frame2);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Camera Show");
for (int i = 1; i < 10; i++)
cout << a[i];
}