본문 바로가기

애니리뷰

opencv의 c++에서의 픽셀 접근

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;
int main(int, char**) {
	Mat img_in = imread("C:/Users/is7se/source/repos/OpencvStudy/OpencvStudy/img/Test.tif",
		IMREAD_GRAYSCALE);
	int H = img_in.rows;
	int W = img_in.cols;
	
	if (img_in.empty()) {
		cout << "Image does not exist." << endl;
		return 1;
	}
	
	else {
		for (int i = 0; i < H; i++)
			img_in.at<uchar>(i, 20) = 255;
	}

	imwrite("Save.tif", img_in);
	return 0;
}

 

 

얼굴

//#include <opencv2/opencv.hpp>
//#include <opencv2/core/core.hpp>
//#include <opencv2/objdetect/objdetect.hpp>
//#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
//
//using namespace cv;
//using namespace std;
//
//#define CV_HAAR_SCALE_IMAGE 2
//
//String face_cascade = "C:/opencv/sources/data/haarcascades/haarcascade_eye.xml";
//String eye_cascade =
//"C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
//CascadeClassifier face;
//CascadeClassifier eye;
//
//void FaceAndEyeDetect(Mat img) {
//    Mat gray;
//    cvtColor(img, gray, COLOR_BGR2GRAY);
//    vector < Rect > face_pos;
//    face.detectMultiScale(gray, face_pos, 1.1, 2, 0 |
//        CASCADE_SCALE_IMAGE, Size(10, 10));
//
//    for (int i = 0; i < face_pos.size(); i++) {
//        rectangle(img, face_pos[i], Scalar(255, 0, 0), 2);
//    }
//
//    /*for (int i = 0; i < face_pos.size(); i++) {
//        vector < Rect > eye_pos;
//        Mat roi = gray(face_pos[i]);
//
//        eye.detectMultiScale(roi, eye_pos, 1.1, 2, 0
//            | CASCADE_SCALE_IMAGE, Size(10, 10));
//
//        for (int j = 0; j < eye_pos.size(); j++) {
//            Point center(face_pos[i].x + eye_pos[j].x + eye_pos[j].width * 0.5,
//                face_pos[i].y + eye_pos[j].y + eye_pos[j].height * 0.5);
//            int radius = cvRound((eye_pos[j].width + eye_pos[j].height) * 0.25);
//            circle(img, center, radius, Scalar(0, 0, 255), 2.8, 0);
//        }
//    }*/
//    namedWindow("얼굴 검출"); imshow("얼굴 검출", img);
//}
//
//int main() {
//    Mat f = imread
//        //("C:/Users/is7se/source/repos/OpencvStudy/OpencvStudy/img/test_nanami.jpg");
//        ("C:/Users/is7se/source/repos/OpencvStudy/OpencvStudy/img/test_nanami2.png");
//    assert(f.data);
//    bool b1 = face.load(face_cascade);
//    bool b2 = eye.load(eye_cascade);
//    assert(b1 && b2);
//    FaceAndEyeDetect(f);
//
//    waitKey();
//    return 0;
//}

mat 처리

#include "utils.h"


Mat A(Mat src)
{
	//
	int min = 2100000000, max = 0;
	Mat dst = src;
	for (int i = 0; i < src.rows; i++) {
		for (int j = 0; j < src.cols; j++) {
			if (src.at<uchar>(i, j) < min) min = src.at<uchar>(i, j);
			if (src.at<uchar>(i, j) >  max) max = src.at<uchar>(i, j);
		}
	}

	for (int i = 0; i < src.rows; i++) {
		for (int j = 0; j < src.cols; j++) {
			dst.at<uchar>(i, j) = 255 * (src.at<uchar>(i, j) - min) / (max - min);
		}
	}

	return dst;
}

Mat B(Mat src)
{
	Mat dst = src;
	for (int i = 0; i < src.rows; i++) {
		for (int j = 0; j < src.cols; j++) {
			if (dst.at<uchar>(i, j) > 128) dst.at<uchar>(i, j) = 255;
			else dst.at<uchar>(i, j) = 0;
		}
	}
	return dst;
}

Mat rotate(Mat src, float theta, string interp_type)
{
	Mat dst(src.rows, src.cols, CV_8UC1, Scalar(128));
	// 최근접: round로 반올림함.
	if (interp_type == "nearest") {
		int x = dst.cols, y = dst.rows;
		int xx = (int)(x * sqrt(2)), yy = (int)(y * sqrt(2));
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				int nx = round((j - x / 2) * cos(theta) - (i - y / 2) * sin(theta) + x / 2);
				int ny = round(-(j - x / 2) * sin(theta) - (i - y / 2) * cos(theta) + y / 2);
				if (nx >=0 && nx < x && ny >=0 && ny <= y)
					dst.at<uchar>(i, j) = src.at<uchar>(ny, nx);
			}
		}
	}
	else {
		int x = dst.cols, y = dst.rows;
		int xx = (int)(x * sqrt(2)), yy = (int)(y * sqrt(2));
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				double nx = ((j - x / static_cast<double>(2)) * cos(theta) - (i - y / static_cast<double>(2)) * sin(theta) + x / static_cast<double>(2));
				double ny = (-(j - x / static_cast<double>(2)) * sin(theta) - (i - y / static_cast<double>(2)) * cos(theta) + y / static_cast<double>(2));

				if (nx >= 0 && nx < x && ny >= 0 && ny <= y) {
					int nx1 = floor(nx), nx2 = ceil(nx), ny1 = floor(ny), ny2 = ceil(ny);
					int A = src.at<uchar>(ny1, nx1), B = src.at<uchar>(ny1, nx2),
						D = src.at<uchar>(ny2, nx1), C = src.at<uchar>(ny2, nx2);
					double subx1 = nx - nx1, subx2 = nx2 - nx;
					double suby1 = ny - ny1, suby2 = ny2 - ny;
					double P = subx2 * A + subx1 * B;
					double Q = subx2 * D + subx1 * C;

					dst.at<uchar>(i, j) = suby2 * P + suby1 * Q;
				}
			}
		}
	}

	return dst;
}

Mat translation(Mat src, float x, float y, string interp_type)
{
	Mat dst(src.rows, src.cols, CV_8UC1, Scalar(128));
	if (interp_type == "nearest") {
		int x = dst.cols, y = dst.rows;
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				int ny = round(i);
				int nx = round(j - 30);
				if (nx >= 0 && nx < x && ny >= 0 && ny <= y)
					dst.at<uchar>(i, j) = src.at<uchar>(ny, nx);
			}
		}
	}
	else {
		int x = dst.cols, y = dst.rows;
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				int ny = i;
				int nx = j - 30;
				if (nx >= 0 && nx < x && ny >= 0 && ny <= y) {
					int nx1 = floor(nx), nx2 = ceil(nx), ny1 = floor(ny), ny2 = ceil(ny);
					int A = src.at<uchar>(ny1, nx1), B = src.at<uchar>(ny1, nx2),
						D = src.at<uchar>(ny2, nx1), C = src.at<uchar>(ny2, nx2);
					int subx1 = nx - nx1, subx2 = nx2 - nx;
					int suby1 = ny - ny1, suby2 = ny2 - ny;
					int P = subx2 * A + subx1 * B;
					int Q = subx2 * D + subx1 * C;

					dst.at<uchar>(i, j) = suby2 * P + suby1 * Q;
				}
			}
		}
	}
	return dst;
}

'애니리뷰' 카테고리의 다른 글

네트워크 5) 4계층(Transport Layer)  (0) 2022.04.16
네트워크 3 ) IP 어드레싱  (0) 2022.04.03
네트워크 2 ) 신호의 전송과 충돌  (0) 2022.03.14
네트워크 1 ) 기초  (0) 2022.03.13
조합 dfs 형식으로 풀기  (4) 2021.10.01