πŸ“˜ Programming/OpenCV

[OpenCV] μ˜μƒμ˜ ν™”μ†Œ(Pixel) κ°’ 읽기

ν•œμ½”λ”© 2020. 9. 20. 12:02
728x90
728x90

 

μ†ŒμŠ€ μ½”λ“œ

sample128.PNG
0.02MB

μœ„ 이미지 νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œν•΄μ€μ‹œλ‹€. μœ„ μ˜μƒμ˜ ν™”μ†Œ 값을 μ½μ–΄μ˜€λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄λ΄…μ‹œλ‹€

 

μž…λ ₯된 μ˜μƒ 파일의 ν™”μ†Œ 값을 좜λ ₯ν•˜λŠ” μ†ŒμŠ€μ½”λ“œμž…λ‹ˆλ‹€.

즉, μ˜μƒμ˜ 각 ν”½μ…€ 값을 λͺ¨λ‘ 좜λ ₯ν•΄μ€λ‹ˆλ‹€.

 

μ•„λž˜λŠ” μ†ŒμŠ€μ½”λ“œμž…λ‹ˆλ‹€. OpenFileDialog() 둜 μ˜μƒ νŒŒμΌμ„ λΆˆλŸ¬μ˜΅λ‹ˆλ‹€.

#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <Windows.h>
#include <string.h>
#include <iostream>

using namespace std;
using namespace cv;

string OpenFileDialog() {
	char name[MAX_PATH] = { 0, };
	OPENFILENAMEA ofn;
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(OPENFILENAMEA);
	ofn.hwndOwner = NULL;
	ofn.lpstrFilter = "λͺ¨λ“ νŒŒμΌ(*.*)\0*.*\0";
	ofn.lpstrFile = name;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "";

	string strName;
	if (GetOpenFileNameA(&ofn)) 
		strName = name;
	
	return strName;
}

void ReadImageToString() {

	// μ˜μƒμ˜ ν™”μ†Œκ°’ 확인
	auto name = OpenFileDialog();

	if (name != "") {
		auto image = imread(name, IMREAD_GRAYSCALE);

		if (image.empty()) {
			cout << "μ˜μƒμ„ μ½λŠ”λ° μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€." << endl;
			exit(1);
		}

		// 관심 μ˜μ—­
		Rect roi(0, 0, 32, 32);
		// μ˜μƒμ—μ„œ κ΄€μ‹¬μ˜μ—­ μΆ”μΆœ
		Mat roi_img = image(roi);

		// μ˜μƒμ˜ κ°’ 좜λ ₯
		cout << "[roi_img] = " << endl;

		for (int i = 0; i < roi_img.rows; i++) {
			for (int j = 0; j < roi_img.cols; j++) {
				cout.width(4);
				cout << (int)roi_img.at<uchar>(i, j);
			}
			cout << endl;
		}

		// μ˜μƒμ— μ‚¬κ°ν˜• 그리기 (κ΄€μ‹¬μ˜μ—­)
		rectangle(image, roi, Scalar(255), 1);

		// μ˜μƒ 좜λ ₯
		imshow("image", image);
		waitKey();
	}
}
void main() {
	ReadImageToString();
}

 

μ‹€ν–‰ κ²°κ³Ό

 

μœ„ 처럼 이미지λ₯Ό 뢈러였면 μ΄λ ‡κ²Œ 좜λ ₯이 λ©λ‹ˆλ‹€.

 

728x90
λ°˜μ‘ν˜•