πŸ“˜ Programming/OpenCV

[OpenCV] μ˜μƒ μŠ€μΌ€μΌλ§(Scailing)

ν•œμ½”λ”© 2020. 12. 30. 14:55
728x90
728x90

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

기쑴에 λ‹€μš΄ λ°›μ•˜λ˜ sample128.png, lenna.png 사진을 ν™œμš©ν•©λ‹ˆλ‹€.

​

μ˜μƒμ˜ μŠ€μΌ€μΌλ§μ„ μ‘°μ ˆν•˜λŠ” μ†ŒμŠ€μ½”λ“œμž…λ‹ˆλ‹€.

 

#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;
}

Mat MyScailing1(Mat image, Size size) {
	// μ˜μƒμ˜ ν™”μ†Œλ₯Ό μƒ˜ν”Œλ§
	Mat ret(size, CV_8UC1, Scalar(0));
	double scaleX = (double)size.width / image.cols;
	double scaleY = (double)size.height / image.rows;

	for (int i = 0; i < image.rows; i++) {
		for (int j = 0; j < image.cols; j++) {
			int x = (int)(j * scaleX);
			int y = (int)(i * scaleY);
			ret.at<uchar>(y, x) = image.at<uchar>(i, j);
		}
	}
	return ret;
}

Mat MyScailing2(Mat image, Size size) {
	// 변경될 μ‚¬μ΄μ¦ˆμ— μ›λž˜ μ˜μƒμœΌλ‘œλΆ€ν„° ν™”μ†Œλ₯Ό μΆ”μΆœ
	Mat ret(size, CV_8UC1, Scalar(0));
	double scaleX = (double)size.width / image.cols;
	double scaleY = (double)size.height / image.rows;

	for (int i = 0; i < ret.rows; i++) {
		for (int j = 0; j < ret.cols; j++) {
			int x = (int)(j * scaleX);
			int y = (int)(i * scaleY);
			ret.at<uchar>(y, x) = image.at<uchar>(i, j);
		}
	}
	return ret;
}

void Scailing() {
	// μŠ€μΌ€μΌλ§
	auto name = OpenFileDialog();

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

		Mat m1, m2, m3, m4, m5;

		resize(image, m1, Size(256, 256), 0, 0);
		imshow("Down", m1);
		waitKey();

		resize(image, m2, Size(1024, 1024), 0, 0);
		imshow("Up", m2);
		waitKey();

		m3 = MyScailing1(image, Size(256, 256));
		imshow("MyDownSampling", m3);
		waitKey();

		m4 = MyScailing1(image, Size(1024, 1024));
		imshow("MyDownSampling", m4);
		waitKey();

		m5 = MyScailing1(image, Size(1024, 1024));
		imshow("MyDownSampling", m5);
		waitKey();
	}
}
int main() {
	Scailing();
}

 


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

​

728x90
λ°˜μ‘ν˜•