일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 환경변수
- MFC
- 인스피론
- 가벼워
- 사용자매크로
- 여객선
- inspiron 15 7590
- VisualStudio
- Jetson Nano
- Thread
- 영상처리
- SetThreadAffinityMask
- Today
- Total
MuTa
OpenCV 기본 본문
MFC 기준
Include/Opencv2폴더 사용 하는 것을 권장
Core : Mat 클래스를 포함한 기본 자료 구조와 여러 함수가 공유하는 클래스
Highgui : 윈도우 화면과 마우스 등을 제어하는 사용자 인터페이스 함수
Imgporc : 영상처리 함수
Features2d : 특징 검출, 기술자, 매칭 함수
Objdetect : 얼굴과 사람을 포함한 다양한 물체 검출 함수
Video : 연속 영상과 모션 추정 함수
Calib3d: 카메라 캘리브레이션과 3차원 복원 함수
Ml : 기계학습
Flann: 군집화와 다차원 공간 탐색
Gpu : GPU 프로그래밍
//MFC + Unicode
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Image Load
CString File.format(_T("C:\\img.bmp"));
USES_CONVERSION;
Mat Image = cv::imread(T2A(File)); //컬러
Mat ImageGray = cv::imread(T2A(File), IMREAD_GRAYSCALE); //Gray
Image Save
CString strSave;
USES_CONVERSION;
strSave=_T("d:\\save.jpg");
imwrite(T2A(strSave), ImageGray);
Create
Mat img2(200,200,CV_8UC1); //CV_8UC1 1채널 RGB
IPLImage <- Mat
Mat matImg;
IplImage* pImg;
pImg = &IplImage(matImg);
IPLImage -> Mat
IplImage* pImage;
Mat matImg;
matImg(pImage);
matImg = cv::cvarrToMat(pImage); //CV 3.0 이상
// roi 설정
Rect roi; -> CRect 로는 안되고,, cv::Rect or cv::Range 를 사용해야 한다.
cv::Rect rRect(pTopLeft, pBottomRight);
cv:Rect rect(x, y, width, height) 구조이니,, 참고
Mat sub_img = img(roi); //메모리공유
Mat sub_img = img(roi).clone(); //별도메모리
Picture_Control 화면 출력
CDC* pDC;
CRect rect;
pDC = m_stDisplay.GetDC();
m_stDisplay.GetClientRect(&rect);
m_pCVCtrl->DisplayImage(pDC, rect);
ReleaseDC(pDC);
void COpenCVCtrl::DisplayImage(CDC* pDC, CRect rect)
{
cv::Size winSize(rect.right, rect.bottom);
// Resize the source to the size of the destination image if necessary
cv::Mat cvImgTmp(winSize, CV_8UC3);
if (m_mImg.size() != winSize)
{
cv::resize(m_mImg, cvImgTmp, winSize);
}
else
{
cvImgTmp = m_mImg;
}
// Rotate the image
cv::flip(cvImgTmp, cvImgTmp, 0);
// Initialize the BITMAPINFO structure
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 24;
bitInfo.bmiHeader.biWidth = winSize.width;
bitInfo.bmiHeader.biHeight = winSize.height;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
// Add header and OPENCV image's data to the HDC
StretchDIBits(pDC->GetSafeHdc(), 0, 0,
winSize.width, winSize.height, 0, 0,
winSize.width, winSize.height,
cvImgTmp.data, &bitInfo, DIB_RGB_COLORS, SRCCOPY);
}
픽셀 연산
cv::threshold(m_matGray,matLoBin, iLow, 255, THRESH_BINARY_INV);
cv::threshold(m_matGray,matHiBin, iHigh,255, THRESH_BINARY);
Mat matRes(m_matOriImage.rows, m_matOriImage.cols, CV_8U, Scalar(0));
for (int j=0; j<m_matOriImage.rows; j++)
for (int i=0; i<m_matOriImage.cols; i++)
{
if((matLoBin.at<uchar>(j,i)==255)||(matHiBin.at<uchar>(j,i)==255))
matRes.at<uchar>(j,i) = 255;
}
for(int j=0; j < image.rows; j++)
for(int i=0; i < image.cols; i++)
{
image.at<Vec3b> (j,i)[0] += 30; //컬러 채널 접근 B
image.at<Vec3b> (j,i)[1] += 20; //G
image.at<Vec3b> (j,i)[2] += 20; //R
}
위처럼 연산하는 경우는 거의 없다..
이미지의 Raw 또는 시작 Pointer를 가지고 활용 하는 방법
Full 이미지 생성 -> Crop 이미지 생성 -> Crop이미지 시작 Pointer 가져오기..
BYTE* pbtImage = new BYTE[sizeF * sizeF];
//주의 – 이때,, pbtImage의 경우,, 메모리 공유를 하게 된다.. 임의로,, btImage를 지우게 되면,, 황이다.
cv::Mat imageFull= cv::Mat(height, width, CV_8U, pbtImage);
cv::Mat imageRoi = imageFull(roi).clone();
BYTE* p0 = imageRoi.ptr<BYTE>(0);
Sobel/ Canny
Mat matSobel, matCanny;
Mat matgray;
matgray = m_matGray.clone();
blur(matgray,matgray,Size(3,3));
Canny(matgray,matCanny, 30, 30*2.5, 3);
Mat gradx, grady;
Sobel(matgray,gradx,CV_16S,1,0,3);
Sobel(matgray,grady,CV_16S,0,1,3);
convertScaleAbs(gradx,gradx); //8 비트양수변환
convertScaleAbs(grady,grady);
addWeighted(gradx,0.5,grady,0.5,0,matSobel); 합치기
Mask Convolution
Mat mask(3,3,CV_32F,Scalar(0)), res;
mask.at<float>(0,0)=-1.0;
mask.at<float>(2,2)=1.0;
filter2D(src,res,CV_16S,mask);
res.convertTo(res,CV_8U,1,128);
'OpenCV' 카테고리의 다른 글
PCA 관련 정보들 (0) | 2016.10.05 |
---|---|
cv::PCA 사용시 EigenValue 소팅 안된 값 찾기 (0) | 2016.10.05 |
Image Watch VS2013 확장 프로그램 (0) | 2016.05.13 |