반응형
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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
int main() {
    IplImage *image;
 
    int height, width;
    uchar *data;
 
    // 흑백은 left01.jpg , 컬러는 aero1.jpg
    image = cvLoadImage("D:/opencv/sources/samples/data/left01.jpg"-1);
    height = image->height;
    width = image->width;
    data = (uchar*)image->imageData;
    
    printf("height :: %d\nwidth :: %d\n", height, width);
    cvNamedWindow("imgWindow", CV_WINDOW_AUTOSIZE);
 
    for (int i = 0; i < height; i++) {
        
        for (int j = 0; j < width; j++) {
            if (j == width / 2) {
 
                data[i*width + j] = 0;
            }
        }
    }
 
    cvShowImage("imgWindow", image);
 
    cvWaitKey(0);
 
    cvReleaseImage(&image);
    
    return 0;
}
cs


위의 코드는 image의 data값을 직접 수정하며 세로 줄을 가운데에 만들어 낼 수 있다.






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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
int main() {
    IplImage *image;
 
    int height, width, channels;
    uchar *data;
 
    // 흑백은 left01.jpg , 컬러는 aero1.jpg
    image = cvLoadImage("D:/opencv/sources/samples/data/aero1.jpg"-1);
    height = image->height;
    width = image->width;
    channels = image->nChannels;
    data = (uchar*)image->imageData;
    
    printf("height :: %d\nwidth :: %d\n", height, width);
    cvNamedWindow("imgWindow", CV_WINDOW_AUTOSIZE);
 
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
            for(int k = ; k < channels; k++)
                if (i == height / 2)
                    data[channels*(i*width + j) + k] = 0;
 
    cvShowImage("imgWindow", image);
 
    cvWaitKey(0);
 
    cvReleaseImage(&image);
    
    return 0;
}
cs


위의 코드는 RGB 형식의 그림일 때 모습이다.


이때 data의 idx 수식은 i*width + j에 channels를 곱하고 k를 더한 channels*(i*width + j) + k가 된다.





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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
int main() {
    IplImage *image;
 
    int height, width;
    uchar *data;
 
    // 흑백은 blox.jpg , 컬러는 aero1.jpg
    image = cvLoadImage("D:/opencv/sources/samples/data/blox.jpg"3);
    height = image->height;
    width = image->width;
    data = (uchar*)image->imageData;
 
    printf("height :: %d\nwidth :: %d\n", height, width);
    cvNamedWindow("imgWindow", CV_WINDOW_AUTOSIZE);
    cvLine(image, cvPoint(image->width / 20), cvPoint(image->width / 2, image->height), CV_RGB(25500), 380);
    cvLine(image, cvPoint(0, image->height / 2), cvPoint(image->width, image->height / 2), CV_RGB(25500), 380);
 
    cvShowImage("imgWindow", image);
 
    cvWaitKey(0);
 
    cvReleaseImage(&image);
 
    return 0;
}
cs


위의 코드는 cvLine 함수를 이용하여 image에 cvPoint를 이용하여 width와 height에 각각 라인을 그려주는 방식이다.









반응형