include opencv cv include opencv highgui include stdlib include stdio

 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
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <QtCore>
IplImage* image = 0;
IplImage* src = 0;
unsigned char getBrightness(char r, char g, char b) {
return (r + r + b + g + g + g) / 6;
}
bool isDark(char r, char g, char b, float avg) {
unsigned char result = (r + r + b + g + g + g) / 6;
return result < avg;
}
int main(int argc, char *argv[])
{
char* filename = "/Users/SergeyRyabko/Desktop/Xrays/2.jpg";
image = cvLoadImage(filename);
CvSize size;
size.height = image->width;
size.width = image->height;
src = cvCreateImage(size, image->depth, image->nChannels);
assert(image != 0);
cvTranspose(image, src);
cvFlip(src, src);
cvTranspose(src, image);
cvFlip(image, image);
float avg = 0;
for(int i = 0; i < image->height; ++i) {
for(int j = 0; j < image->nChannels * image->width; j+=image->nChannels) {
unsigned char red = image->imageData[i * image->widthStep + j + 2];
unsigned char green = image->imageData[i * image->widthStep + j + 1];
unsigned char blue = image->imageData[i * image->widthStep + j];
avg += getBrightness(red, green, blue);
}
}
avg /= image->height * image->width;
for(int i = 0; i < image->height; ++i) {
for(int j = 0; j < image->nChannels * image->width; j+=image->nChannels) {
unsigned char red = image->imageData[i * image->widthStep + j + 2];
unsigned char green = image->imageData[i * image->widthStep + j + 1];
unsigned char blue = image->imageData[i * image->widthStep + j];
if(!isDark(red, green, blue, avg)) {
image->imageData[i * image->widthStep + j + 2] = 0;
image->imageData[i * image->widthStep + j + 1] = 0;
image->imageData[i * image->widthStep + j] = 255;
}
}
}
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvShowImage("Original", image);
cvWaitKey(0);
cvReleaseImage(&image);
cvReleaseImage(&src);
cvDestroyWindow("Original");
return 0;
}