1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
int main(int argc, char* argv[]) {
IplImage* image = cvLoadImage("C:\\Mohamed\\cp\\docs\\DZ.jpg");
// iterate over the image data to look for very red pixels
int height = image->height;
int width = image->width;
int step = image->widthStep;
if(image->depth != IPL_DEPTH_8U) {
printf("format erroné %d\n", image->depth);
return -1;
}
unsigned char* data = (unsigned char*)image->imageData;
int red_min = 150;
int green_max = 100;
int blue_max = 100;
int found = 0;
for(int j = 0; j < height; j++) {
for(int i = 0; i < width; i++) {
if((data[j * step + i * 3 + 2] > red_min
&& data[j * step + i * 3 + 1] < green_max
&& data[j * step + i * 3 + 0] < blue_max) || 0) {
printf("found very red pixel at %4d %4d: (%3d %3d %3d)\n",
j, i,
data[j * step + i * 3 + 2], // red
data[j * step + i * 3 + 1], // green
data[j * step + i * 3 + 0] // blue
);
found ++;
}
}
}
printf("angle %d red-ish pixels. (%.1f%%)\n",
found,
(double)found / (double)(width * height) * 100.);
IplImage* color_dst = cvCreateImage(cvGetSize(image), 8, 3 );
cvCopy(image, color_dst);
IplImage* gray_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor(image, gray_image, CV_RGB2GRAY);
CvMemStorage* storage = cvCreateMemStorage(0);
// 2 pixel distance resolution
// 2 degree angular resolution
CvSeq* lines = cvHoughLines2(gray_image, storage, CV_HOUGH_STANDARD, 1, CV_PI/180. * 2, 100);
for(int i = 0; i < MIN(lines->total,10); i++) {
float* line = (float*)cvGetSeqElem(lines, i);
float rho = line[0];
float theta = line[1];
printf("line %d: rho: %.1f, theta: %.1fdeg\n", i + 1, rho, theta / CV_PI * 180.);
CvPoint pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a * rho, y0 = b * rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
cvLine(color_dst, pt1, pt2, CV_RGB(0,255,0), 1, 4);
}
cvNamedWindow("input", 1 );
cvShowImage("input", image);
cvNamedWindow("output", 1 );
cvShowImage("output", color_dst);
while(cvWaitKey(100) != 27);
} |
Partager