본문 바로가기

OpenCV

ch04 keyboard

#python code 

import cv2

def main():
    img = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch04\\keyboard\\lenna.bmp")

    if img is None:
        print("Image load failed!")
        return -1

    cv2.namedWindow("img")
    cv2.imshow("img", img)

    while True:
        keycode = cv2.waitKey()

        if keycode == ord('i') or keycode == ord('I'):
            img = ~img
            cv2.imshow("img", img)
        elif keycode == 27 or keycode == ord('q') or keycode == ord('Q'):
            break

    cv2.destroyAllWindows()
    return 0

if __name__ == "__main__":
    main()

// C++ code

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

using namespace cv;
using namespace std;

int main(void)
{
	Mat img = imread("lenna.bmp");

	if (img.empty()) {
		cerr << "Image load failed!" << endl;
		return -1;
	}

	namedWindow("img");
	imshow("img", img);

	while (true) {
		int keycode = waitKey();

		if (keycode == 'i' || keycode == 'I') {
			img = ~img;
			imshow("img", img);
		}
		else if (keycode == 27 || keycode == 'q' || keycode == 'Q') {
			break;
		}
	}

	return 0;
}

C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저


 

'OpenCV' 카테고리의 다른 글

ch04 storage  (0) 2024.05.19
ch04 mouse  (0) 2024.05.19
ch04 drawing  (0) 2024.05.19
ch03 ScalarOp  (0) 2024.05.19
ch03 MatOp  (0) 2024.05.19