4. Threshold 와 트랙바
트랙바 만들기
createTrackbar("트랙바 이름", "트랙바를 적용할 이미지", 최소값, 최대값);
setTrackbarPos("트랙바 이름", "트랙바를 적용할 이미지", Default 값);
getTrackbarPos("트랙바 이름", "트랙바를 적용할 이미지");
코드
#include<iostream>
#include<opencv2\core.hpp>
#include<opencv\cv.h>
#include<opencv2\highgui.hpp>
#include<opencv2\imgproc.hpp>
#define MAX 255
using namespace cv;
using namespace std;
int main() {
Mat image = imread("image/flower.jpg", 0);
Mat thresholded;
int thredshold;
if (!image.data)
{
std::cout << "open failed" << std::endl;
return 0;
}
namedWindow("Binary Image");
//트랙바 만들기 and 세팅하기
createTrackbar("thredshold", "Binary Image", &thredshold, MAX);
setTrackbarPos("thredshold", "Binary Image", 100);
while (true) {
//트랙바 위치 얻어와서 threshold값에 저장하기
thredshold = getTrackbarPos("thredshold", "Binary Image");
threshold(image, thresholded, MAX - thredshold, MAX, THRESH_BINARY);
imshow("Binary Image", thresholded);
int key = waitKey(1);
if (key == 'q') break;
}
waitKey(0);
return 0;
}