OpenCV Color recognition and operation

Source: Internet
Author: User
Tags scalar

#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;
/* Problems facing how to save the plotted curve. */
struct Gfpoint {
int x = 0;
int y = 0;
int OK = 0;
}GFPOINT[9];
Gfpoint First_point, Last_point;
Frame2 looking for the center point of the pixel that identifies the object
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++) {
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 (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);
}
Connect the two points together.
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
}
}
Cvline (frame, centerf, Centerl, Scalar (0, 0, 255), 10);
}
Overlay frame with 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 saturation
PRT[3 * x + 2] = = 255)//v luminosity
{
Continue
}
Else
{
FPRT[3 * x] = prt[3 * x];
FPRT[3 * x + 1] = prt[3 * x + 1];
FPRT[3 * x + 2] = prt[3 * x + 2];
}
}
}
}
Find the first point
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-&GT;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 hue
PRT[3 * x + 1] = 255;//s saturation
PRT[3 * x + 2] = 255;//v lightness
}
}
}
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&LT;PRT[3 * x])
{
Top_h = prt[3 * x];
}
if (LOW_H&GT;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];
}

}
}
}
Corrosion and swelling
void Gferodeanddilate (iplimage* img)
{
Cverode (IMG, IMG, NULL, 1); Corrosion
Cvdilate (IMG, IMG, NULL, 1); Expansion
}
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
}
}
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
}
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)
{
IPRT[3 * x] = 255;//h hue
IPRT[3 * x + 1] = 255;//s saturation
IPRT[3 * x + 2] = 255;//v lightness
}
}
}
}
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);
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;
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;
Size size (120, 120);
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 <; i++)
cout << A[i];
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.