RGB이미지를 HSV이미지, 흑백이미지로 변환하기 (cvCvtColor 함수)



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *hsvImage = 0*grayImage;
    IplImage *srcImage = cvLoadImage("D:/study/testimage.jpg"-1);
    
 
    //create image
    hsvImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
    grayImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 1);    //흑백영상으로 할것이므로 채널을 1로 설정
 
 
    //영상변환
    //cvCvtColor(원본이미지, 결과이미지, 변환모드)
    //변환모드 : CV_<src color code>2<dst color code>
    //color code : RGB, BGR, GRAY, HSV, YcrCb, XYZ, Lab, Luv, HLS
    cvCvtColor(srcImage, hsvImage, CV_RGB2HSV);
    cvCvtColor(srcImage, grayImage, CV_RGB2GRAY);
 
 
 
    //create window
    cvNamedWindow("RGB image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("HSV image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("GRAY image", CV_WINDOW_AUTOSIZE);
    //show window
    cvShowImage("RGB image", srcImage);
    cvShowImage("HSV image", hsvImage);
    cvShowImage("GRAY image", grayImage);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage);
    cvReleaseImage(&hsvImage);
    cvReleaseImage(&grayImage);
}
cs



결과



이진영상 만들기 (cvThreshold 함수)

이진영상 : 0, 255로만 이루어진 영상



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *resultImage64, *resultImage128, *resultImage172;
    double threshold1 = 64, threshold2 = 128, threshold3 = 172;    //임계값 : 기준이 되는 픽셀값
 
    IplImage *srcImage = cvLoadImage("D:/study/testimage.jpg"-1);
 
    //create image
    resultImage64 = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
    resultImage128 = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
    resultImage172 = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
 
 
 
    //이진영상 생성
    //cvThreshold(원본 이미지, 결과 이미지, 임계값, 최대 명암값, 이진화 방식)
    cvThreshold(srcImage, resultImage64, threshold1, 255, CV_THRESH_BINARY);
    cvThreshold(srcImage, resultImage128, threshold2, 255, CV_THRESH_BINARY);
    cvThreshold(srcImage, resultImage172, threshold3, 255, CV_THRESH_BINARY);
    //이진화 방식
    //CV_THRESH_BINARY : 임계값초과 255, 임계값이하 0
    //CV_THRESH_BINARY_INV : 임계값초과 0, 임계값이하 255
    //CV_THRESH_TRUNC : 임계값초과-임계값, 임계값이하-원래픽셀값
    //CV_THRESH_TOZERO : 임계값초과-원래픽셀값, 임계값이하-0
    //CV_THRESH_TOZERO_INV : 임계값초과-0, 임계값이하-원래픽셀값
 
 
 
    //create window 
    cvNamedWindow("source", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("binary T=64", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("binary T=128", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("binary T=172", CV_WINDOW_AUTOSIZE);
    //show window
    cvShowImage("source", srcImage);
    cvShowImage("binary T=64", resultImage64);
    cvShowImage("binary T=128", resultImage128);
    cvShowImage("binary T=172", resultImage172);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage);
    cvReleaseImage(&resultImage64);
    cvReleaseImage(&resultImage128);
    cvReleaseImage(&resultImage172);
}
 
cs



결과




이미지 반전하기 (cvNot 함수)

영상 반전 : 영상 최대 명암값에서 각 픽셀의 명암값을 뺀다



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *srcImage = cvLoadImage("D:/study/testimage.jpg"-1), *resultImage;
 
    //image 객체생성
    resultImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
 
 
    //이미지 반전
    cvNot(srcImage, resultImage);
    //픽셀로 이미지 반전하기
    /*
    int width = srcImage->width;
    int height = srcImage->height;
    int channels = srcImage->nChannels;
    uchar *srcData = (uchar*)srcImage->imageData;
    uchar *resultData = (uchar*)resultImage->imageData;
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            for (int c = 0; c < channels; c++) {
                int index = h*width*channels + w*channels + c;
                resultData[index] = 255 - srcData[index];
            }
        }
    }
    */
 
 
    //create window
    cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("reverse", CV_WINDOW_AUTOSIZE);
 
    //show window
    cvShowImage("original", srcImage);
    cvShowImage("reverse", resultImage);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage);
    cvReleaseImage(&resultImage);
}
 
cs



결과



FloatingActionButton 사용하기



xml)

CoordinatorLayout 으로 감싸주는 것이 좋다

플로팅 액션버튼의 layout_width, layout_height 속성에 크기를 지정할 수 있긴 하지만 너무 크거나 작으면 적용되지 않는다

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
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:fitsSystemWindows="true">
 
    <RelativeLayout
        android:id="@+id/boxMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/map"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>
 
        <!-- 플로팅 액션 버튼 추가 -->
        <!--
        1. 버튼 색깔 바꿀려면 theme 옵션을 사용하여 colorAccent 색깔을 바꾼다
        2. 크기를 변경할려면 fabSize 옵션을 사용한다 (normal , mini)
        3. 이미지 변경은 src 옵션에서 한다
        -->
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFAB"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="16dp"
            app:theme="@style/AppTheme"
            app:fabSize="normal"
            android:elevation="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_dialog_map" />
    </RelativeLayout>
 
</android.support.design.widget.CoordinatorLayout>
 
cs



java)

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.ghj.floatingbtnex;
 
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
 
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
 
    //GoogleMap 객체
    GoogleMap googleMap;
    LocationManager locationManager;
    RelativeLayout boxMap;
    //나의 위도 경도 고도
    double mLatitude;  //위도
    double mLongitude; //경도
 
 
    FloatingActionButton btnFAB;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        boxMap = (RelativeLayout)findViewById(R.id.boxMap);
        btnFAB = (FloatingActionButton)findViewById(R.id.btnFAB);
        btnFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this"Click...", Toast.LENGTH_SHORT).show();
            }
        });
 
        //LocationManager
        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        mLatitude = 37.5197889;
        mLongitude = 126.9403083;
 
 
        //GPS가 켜져있는지 체크
        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            //GPS 설정화면으로 이동
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
            finish();
        }
 
        //마시멜로 이상이면 권한 요청하기
        if(Build.VERSION.SDK_INT >= 23){
            //권한이 없는 경우
            if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(MainActivity.thisnew String[]{Manifest.permission.ACCESS_COARSE_LOCATION , Manifest.permission.ACCESS_FINE_LOCATION} , 1);
            }
            //권한이 있는 경우
            else{
                createMap();
            }
        }
        //마시멜로 아래
        else{
            createMap();
        }
    }
 
    //권한 요청후 응답 콜백
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //ACCESS_COARSE_LOCATION 권한
        if(requestCode==1){
            //권한받음
            if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                createMap();
            }
            //권한못받음
            else{
                Toast.makeText(this"권한없음", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }
 
    //맵생성
    public void createMap(){
        //맵생성
        SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        //콜백클래스 설정
        mapFragment.getMapAsync(MainActivity.this);
    }
 
    //구글맵 생성 콜백
    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
 
        //지도타입 - 일반
        this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
 
        //나의 위치 설정
        LatLng position = new LatLng(mLatitude , mLongitude);
 
        //화면중앙의 위치와 카메라 줌비율
        this.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
    }
}
 
cs



styles.xml)

1
2
3
4
5
6
7
8
9
10
11
<resources>
 
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#489CFF</item>
        <item name="colorPrimaryDark">#489CFF</item>
        <item name="colorAccent">#489CFF</item>
    </style>
 
</resources>
cs



AndroidManifest.xml)

1
2
3
4
5
6
7
...
<!-- Google Map -->
<meta-data android:name="com.google.android.geo.API_KEY" android:value="" />
 
...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
cs



gladle)

라이브러리 버전에 주의할것

design 라이브러리와 appcompat-v7 라이브러리의 하위버전도 맞춰주는 것이 좋다.

ex) appcompat-v7 의 버전이 24.2.1 이면 24.2 까지는 맞춰줄것

1
2
3
4
5
6
7
dependencies {
    ...
    
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.0'
    compile 'com.google.android.gms:play-services-maps:9.8.0'
}
cs



결과

지도위에 플로팅 액션 버튼이 보여진다


두 이미지 합성하기(뺄셈 cvSub 함수)



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
 
    IplImage *srcImage1, *srcImage2, *resultImage = 0;
 
    srcImage1 = cvLoadImage("D:/study/Lena.jpg"-1);
    srcImage2 = cvLoadImage("D:/study/background.jpg"-1);
 
 
 
    //두영상의 덧셈, 뺄셈, 곱셈, 나눗셈을 이용하여 이미지 합성이나 특수효과가 가능하다
    //이미지객체 생성
    resultImage = cvCreateImage(cvGetSize(srcImage1), IPL_DEPTH_8U, 3);
    //cvSub(첫번째 소스이미지, 두번째 소스이미지, 결과이미지, 마스크) : 두 이미지 더하기
    cvSub(srcImage1, srcImage2, resultImage, NULL);
 
    
    //함수 사용하지 않고 픽셀을 직접 계산했을때
    /*
    int width = srcImage1->width;
    int height = srcImage2->height;
    int channels = srcImage1->nChannels;
    uchar *srcData1 = (uchar*)srcImage1->imageData;
    uchar *srcData2 = (uchar*)srcImage2->imageData;
    uchar *resultData = (uchar*)resultImage->imageData;
    int temp = 0;
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            for (int c = 0; c < channels; c++) {
                int index = h*width*channels + w*channels + c;
                //두 픽셀값을 뺀다
                temp = srcData1[index] - srcData2[index];
                if (temp < 0)
                    resultData[index] = 0;
                else
                    resultData[index] = temp;
            }
        }
    }
    */
 
 
    //create window
    cvNamedWindow("source image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("background image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("result image", CV_WINDOW_AUTOSIZE);
    //show window
    cvShowImage("source image", srcImage1);
    cvShowImage("background image", srcImage2);
    cvShowImage("result image", resultImage);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage1);
    cvReleaseImage(&srcImage2);
    cvReleaseImage(&resultImage);
}
cs



결과

흰색이 255, 검색은이 0이므로 백그라운드의 흰색부분은 검은색으로 보이고 검은색 부분은 그대로 보인다



두 이미지 합성하기(덧셈 cvAdd 함수)

같은크기의 두이미지의 픽셀값을 더한다 (두 이미지는 픽셀 array의 길이가 같아야 한다)


c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
 
    IplImage *srcImage1, *srcImage2, *resultImage = 0;
 
    srcImage1 = cvLoadImage("D:/study/Lena.jpg"-1);
    srcImage2 = cvLoadImage("D:/study/background.jpg"-1);
 
 
    
    //두영상의 덧셈, 뺄셈, 곱셈, 나눗셈을 이용하여 이미지 합성이나 특수효과가 가능하다
    //이미지객체 생성
    resultImage = cvCreateImage(cvGetSize(srcImage1), IPL_DEPTH_8U, 3);
    //cvAdd(첫번째 소스이미지, 두번째 소스이미지, 결과이미지, 마스크) : 두 이미지 더하기
    cvAdd(srcImage1, srcImage2, resultImage, NULL);
 
 
 
    //create window
    cvNamedWindow("source image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("background image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("result image", CV_WINDOW_AUTOSIZE);
    //show window
    cvShowImage("source image", srcImage1);
    cvShowImage("background image", srcImage2);
    cvShowImage("result image", resultImage);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage1);
    cvReleaseImage(&srcImage2);
    cvReleaseImage(&resultImage);
}
cs



결과




이미지를 선명하게 하기(cvMul 함수)



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *srcImage, *tempImage=0*multiImage=0*divImage=0;
 
 
    srcImage = cvLoadImage("D:/study/testimage.jpg"-1);    //이미지 로드
 
 
    //이미지객체 생성
    //cvCreateImage(이미지크기, Depth, 채널수)
    tempImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
    multiImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
    divImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);
 
    //cvSet(결과이미지, 픽셀값, 마스크) : 행렬이나 이미지를 주어진 픽셀값으로 채움
    cvSet(tempImage, cvScalarAll(1), NULL);
 
    //곱셈 : 명암대비 증가 -> 선명한 영상
    //나눗셈 : 명암대비 감소
    //cvMul(첫번째이미지, 두번째이미지, 결과이미지, 곱하는 상수)
    cvMul(srcImage, tempImage, multiImage,1.5);
    cvMul(srcImage, tempImage, divImage, 1/2.0);
 
 
 
    //create window 
    cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("multiply", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("divide", CV_WINDOW_AUTOSIZE);
 
    //show window
    cvShowImage("original", srcImage);
    cvShowImage("multiply", multiImage);
    cvShowImage("divide", divImage);
 
    cvWaitKey(0);
 
    //release image
    cvReleaseImage(&srcImage);
    cvReleaseImage(&multiImage);
    cvReleaseImage(&divImage);
}
 
cs



결과




이미지를 밝게, 어둡게하기



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *srcImage;
    IplImage *brightImage, *darkImage;
 
    srcImage = cvLoadImage("D:/study/testimage.jpg"-1);    //이미지 로드
 
 
    brightImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);    //이미지객체1
    darkImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 3);    //이미지객체2
 
 
    //덧셈 : 이미지를 밝게한다
    //뺄셈 : 이미지를 어둡게한다
    //cvAddS , cvSubS(원본이미지, 더하고자하는 상수값, 목적이미지, 마스크)
    cvAddS(srcImage, CV_RGB(100100100), brightImage, NULL);    //brightImage는 100만큼 밝아진 이미지가 된다
    cvSubS(srcImage, cvScalarAll(100), darkImage, NULL);    //dark는 100만큼 어두워진 이미지가 된다
    
 
 
    //create window
    cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("bright", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("dark", CV_WINDOW_AUTOSIZE);
    //show window
    cvShowImage("original", srcImage);
    cvShowImage("bright", brightImage);
    cvShowImage("dark", darkImage);
 
    cvWaitKey(0);
 
    cvReleaseImage(&srcImage);
    cvReleaseImage(&brightImage);
    cvReleaseImage(&darkImage);
}
cs



결과



컬러 이미지를 흑백 이미지로 변환하기



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *image;
 
    image = cvLoadImage("D:/study/testimage.jpg"-1);     //이미지 로드
 
    int height = image->height; //이미지 높이
    int width = image->width;    //이미지 너비
    int channels = image->nChannels;    //이미지 채널
    uchar* data = (uchar*)image->imageData; //이미지 픽셀데이터
 
    //컬러영상 -> 흑백영상으로 변환
    //공식 흑백영상 = 0.333*B + 0.333*G + 0.333*R
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            double blue = 0.333*data[h*width*channels + w*channels + 0];
            double green = 0.333*data[h*width*channels + w*channels + 1];
            double red = 0.333*data[h*width*channels + w*channels + 2];
 
            int intensity = (int)(blue + green + red);    //각채널의 평균값
 
            for (int c = 0; c < channels; c++) {
                data[h*width*channels + w*channels + c] = intensity;
            }
        }
    }
 
 
    cvNamedWindow("image", CV_WINDOW_AUTOSIZE);    //윈도우 생성
    cvShowImage("image", image);    //윈도우에 이미지 표시
 
 
    cvWaitKey(0);
 
    cvReleaseImage(&image);
}
cs



결과



이미지 가운데에 흰색 선긋기 예제



c++)

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
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
 
void main() {
    IplImage *image;
 
    image = cvLoadImage("D:/study/testimage.jpg"-1);
 
    int height = image->height;    //영상 높이(단위 : 픽셀)
    int width = image->width;    //영상 너비(단위 : 픽셀)
    int channel = image->nChannels;    //영상 채널
    uchar* data = (uchar*)image->imageData; //영상데이터의 포인터    
 
 
    //가운도에 흰선을 긋는다
    int line = width/2;
 
    //크기 (m x n) , 위치 (i x j) , 채널 k번째 영상의 1차원 배열인덱스 구하는 공식
    //index = j*m*채널수 + i*채널수 + k
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            for (int c = 0; c < channel; c++) {
                if (w == line) {
                    data[h*width*channel + w*channel + c] = 255;
                }
            }
        }
    }
 
    cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
    cvShowImage("image", image);    //윈도우에 이미지 출력
 
    cvWaitKey(0);
 
    cvReleaseImage(&image);
}
cs



결과




+ Recent posts