2014年2月16日日曜日

指定エリア内のカラー平均グラフの生成

ManyAreaValue.h
--------------------------------

#pragma once
#include "cvbase.h"
#include <vector>

using namespace std;

class Area
{
public:
Area(void);
CvPoint start;
CvPoint end;
int sum[3];
int before[3];
};
class ManyAreaValue :
public CVBase
{
public:
ManyAreaValue(void);
~ManyAreaValue(void);
vector<Area *> areas;
CvPoint pStart;
int speed;

IplImage *graph;

FILE *fp;
cv::VideoCapture cap;
bool mouse;

int count;
int position;
void mouseEvent (int event, int x, int y, int flags, void *param);
void mouseEvent2 (int event, int x, int y, int flags, void *param);
void init(void);
void mainloop(void);

CvScalar createColor(int colorNum);

//void term(void);

void calucurateAreaRGB(IplImage *img);
void printAreaRGB(void);


};



ManyAreaValue.cpp
-----------------------------

#include "ManyAreaValue.h"

void on_mouse (int event, int x, int y, int flags, void *param = NULL);
void on_mouse2 (int event, int x, int y, int flags, void *param = NULL);

Area::Area(void)
{
for (int j=0;j<3;j++){
this->sum[j] = -1;
}

}

ManyAreaValue::ManyAreaValue(void)
{
count = 0;
graph = cvCreateImage( cvSize( 1200, 255*4), IPL_DEPTH_8U, 3);
for (int i=0;i<4;i++){
cvLine (graph, cvPoint(0,255*i), cvPoint(1200,255*i), CV_RGB (    0,  0,  0), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+100), cvPoint(1200,255*i+100), CV_RGB (    100,  100,  100), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+200), cvPoint(1200,255*i+200), CV_RGB (    100,  100,  100), 1, CV_AA, 0);
}
}

ManyAreaValue::~ManyAreaValue(void)
{
}

void ManyAreaValue::mouseEvent (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
pStart.x = x;
pStart.y = y;
break;
case CV_EVENT_LBUTTONUP:
Area *area = new Area();
CvPoint pEnd;
area->end.x = x;
area->end.y = y;
area->start = pStart;
areas.push_back(area);
break;
}
}

void ManyAreaValue::mouseEvent2 (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
break;
case CV_EVENT_LBUTTONUP:
int setPos = (int)position/1200;
setPos += x;
count = x;
position = setPos;
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, setPos*speed );
//frameNum = x*10;
break;
}
}

void ManyAreaValue::calucurateAreaRGB(IplImage *img)
{
for (int i=0;i<areas.size();i++){
uchar p[3];
for (int j=0;j<3;j++){
areas[i]->before[j] = areas[i]->sum[j] ;
areas[i]->sum[j] = 0;
}
int areaSize = (areas[i]->end.y-areas[i]->start.y) * (areas[i]->end.x-areas[i]->start.x);
for (int y = areas[i]->start.y; y < areas[i]->end.y; y++) {
for (int x = areas[i]->start.x; x < areas[i]->end.x; x++) {
for (int j=0;j<3;j++){
p[0] = img->imageData[img->widthStep * y + x * 3 + 2 - j];
areas[i]->sum[j] += p[0];
}
}
}
for (int j=0;j<3;j++){
areas[i]->sum[j] /= areaSize;
}
}
}

CvScalar ManyAreaValue::createColor(int colorNum)
{
if (colorNum == 0){
return CV_RGB (  255,    0,  0);
}else if(colorNum == 1){
return CV_RGB (    0,  255,  0);
}else if (colorNum == 2){
return CV_RGB (    0,    0, 255);
}
return CV_RGB (    255,  255, 255);
}

void ManyAreaValue::printAreaRGB(void)
{
double msec = cvGetCaptureProperty(this->capture,CV_CAP_PROP_POS_MSEC);
fprintf(this->fp,"%lf,",msec);
for (int i=0;i<areas.size();i++){
for (int j=0;j<3;j++){
if (areas[i]->before[j] !=-1){

cvLine(graph,
cvPoint(count-1,255*i),
cvPoint(count,255*i+255),
CV_RGB (    0,  0,  0),
2, CV_AA, 0);
fprintf(this->fp,"%d,",areas[i]->sum[j]);
cvLine (graph,
cvPoint(count-1,255*i+areas[i]->before[j]),
cvPoint(count,255*i+areas[i]->sum[j]),
createColor(j),
2, CV_AA, 0);
}
}
}
fprintf(this->fp,"\n");
cvShowImage ("Graph",this->graph);
}

void ManyAreaValue::init(void)
{
FILE *initfp;
initfp = fopen("ini/ManyAreaValue.txt", "r");
char fileName[256];
int mode;
fscanf(initfp,"%d",&mode);
if (mode == 1){
fscanf(initfp,"%s",&fileName);
fscanf(initfp,"%d",&speed);
this->capture = cvCaptureFromFile(fileName);//"C:\\Users\\aiko\\Documents\\Visual Studio 2010\\Projects\\OpenCVBase\\1_01_H_131129153000.avi");
int areaNum;
fscanf(initfp,"%d",&areaNum);
for (int i=0;i<areaNum;i++){
Area *area = new Area();
fscanf(initfp,"%d",&area->start.x);
fscanf(initfp,"%d",&area->start.y);
fscanf(initfp,"%d",&area->end.x);
fscanf(initfp,"%d",&area->end.y);
this->areas.push_back(area);
}
}else{
this->capture = this->fromCamera();
}

cvNamedWindow( "Capture", 1 );
cvSetMouseCallback ("Capture", on_mouse);
cvNamedWindow( "Graph", 1 );
cvMoveWindow("Graph",0,0);

cvSetMouseCallback ("Capture", on_mouse);
this->fp = fopen("nyan.txt","w");
double fps = cvGetCaptureProperty(this->capture,CV_CAP_PROP_FPS);

}

void ManyAreaValue:: mainloop(void)
{
while (1) {
count++;
position++;
if(count>1200)count = 0;
for (int i=0;i<speed;i++){
this->frame = cvQueryFrame (this->capture);
}
if ( this->frame == NULL ){
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, 0.0 );
this->frame = cvQueryFrame(this->capture);
continue;
}
if (this->areas.size() > 0){
this->calucurateAreaRGB(this->frame);
this->printAreaRGB();
for(int i=0;i<this->areas.size();i++)
this->drawLine(this->frame,this->areas[i]->start,this->areas[i]->end,CV_RGB (255, 0, 0),2);
}
cvShowImage ("Capture",this->frame);
ch = cvWaitKey ( 1 ); // 0 はディレイ時間 (ミリ秒単位)
if (ch == '\x1b') {
// ESC キー
break;
    }
  }
}

0 件のコメント:

コメントを投稿