본문 바로가기
대외활동/시스템프로그래밍

라즈베리파이 opencv

by hoshi03 2024. 10. 1.

이미지와 텍스트를 5초간 띄우는 코드

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <iostream>


using namespace cv;

int main(){
    Mat img;
    img = imread("/home/pi/opencv/image/buddha.jpg");
    putText(img, "Hello bird!",Point(60,50),FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,0),1,LINE_AA,false);
    rectangle(img, Rect(30,25,350,30), Scalar(0,0,255));
    namedWindow("Hello");
    imshow("hello", img);
    waitKey(5000);
    return 0;
}

 

컴파일 할때 opencv4 쓴다는 거 명시해야됨

g++ hellocv.cpp -o 1 $(pkg-config --cflags --libs opencv4)

 

• 디졸브(한 이미지에서 다른 이미지로 이동)

#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;
 
#define DissolveLevel 100
#define Duration 1000

int main()
{
Mat f1=imread("/home/pi/opencv/image/girl.jpg");
Mat f2=imread("/home/pi/opencv/image/buddha.jpg");

assert(!f1.empty() && !f2.empty() && f1.size()==f2.size());

namedWindow("desolve");
for(double alpha=0.0; alpha<=1.0; alpha+=1.0/DissolveLevel) {
Mat blend = (1-alpha)*f1 + alpha*f2; 
imshow("desolve", blend); 
waitKey(Duration/DissolveLevel); 
}

waitKey();
return 0;
}

 

• 디졸브한 이미지 트랙바로 섞을 위치 지정하기

#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;

#define DissolveLevel 100

int pos=0;  // 트랙바 초기 위치
Mat f1, f2;
Mat roi1, roi2; // f1과 f2를 위한 ROI (region of interest)

void dissolveOnTrackbar(int, void*);

int main()
{
Mat f1=imread("/home/pi/opencv/image/bike.jpg");
Mat f2=imread("/home/pi/opencv/image/holse.jpg");
assert(!f1.empty() && !f2.empty());

Size s1=f1.size(), s2=f2.size();
Size s((s1.width<s2.width)? s1.width:s2.width, (s1.height<s2.height)? s1.height:s2.height); 

Rect rect1, rect2; 
rect1.x=(s1.width<s.width)? 0:(s1.width-s.width)/2;
rect1.y=(s1.height<s.height)? 0:(s1.height-s.height)/2;
rect2.x=(s2.width<s.width)? 0:(s2.width-s.width)/2;
rect2.y=(s2.height<s.height)? 0:(s2.height-s.height)/2;
rect1.width=rect2.width=s.width;
rect1.height=rect2.height=s.height;

roi1=f1(rect1); 
roi2=f2(rect2);

namedWindow("desolve");
createTrackbar("mix","desolve",&pos,DissolveLevel,dissolveOnTrackbar); 
imshow("desolve", roi1); 

waitKey();
return 0;
}

void dissolveOnTrackbar(int pos, void *) 
{
double alpha=((double)pos/DissolveLevel); 
Mat blend=(1-alpha)*roi1+alpha*roi2; 
imshow("desolve", blend);
}