TIL Day 20

Jason Jongyoub Lee·2023년 10월 25일
0

OpenCV

목록 보기
2/11

2023.10.17

Today's agenda
1. Open Image and Write Image
2. OpenCV primary class

1. Open Image and Write Image

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

using namespace std;
using nsmaepsae cv;			// if do not use cv all of opencv code need cv::

int main(){
	Mat image = imread("/source/of/lenna.bmp");
    
    if(image.empty()){		// Image load failed
    	cerr << "Image load failed" << "\n";
       	return -1;
    }
    
    imwrite("/source/of/filepath/for/image.bmp", image);
    
    namedWindow("image");		// window named which image will show
    imshow("image", image);		// show image which opened to window
    waitKey();					// wait for the key press from keyboard
    destroyAllWindows();		// destroy all windows which opened
}
Mat imread(const String& filename, int flags = IMREAD_COLOR);
  • parameters
    filename : path to filename to read(relative path can be applied)
    flags : option flag for image read

    IMREAD_UNCHANGEDread image from image's setting
    IMREAD_GRAYSCALEread image for 1 channel grayscale
    IMREAD_COLORread image for 3 channel color
  • return value : Mat type object variable

bool Mat::empty() const

return value : return 0 when rows, cols, data is 0
if data exist, return true

bool imwrite(const String &filename, InputArray img, const std::vector<int>& params = std::vector<int>());
  • parameters
    filename : path to filename to write(relative path can be applied)
    img : image data(Mat object)
    params : file saveing options
  • return value : if success to write return true, else false
void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
  • open window by given name
void destroyWindow(const Strig& winname);
void destroyAllWindows();
  • close given name of window or all windows
void imshow(const String& winname, InputArray mat);
  • present given mat image data to give window name
void waitKey(int delay = 0);
  • wait for the given delay(ms)

** imshow() and waitKey() is almost pair of code. Actual image show is run after call waitKey()

2. OpenCV Primary class

Scala class

  • double type array size by 4 class
  • present pixel values under 4 channel images
  • can access each elements by [] operator

Mat class

  • 1 channel n dimension or multi-channel matrix class

  • Using constructor size of matrix, data type, channel number and initial value can be declared

  • copy constructor, & operator run for swallow copy

  • Mat::copyTo(), Mat::clone() for deep copy

  • multiple four basic operation and << operator support

    Mat class depth

0개의 댓글