安裝OpenCV3.4.3(搭配Visual Studio professional 2017)
※刪除線的部分是我走過的冤枉路,若你有自虐傾向想再走一次也無防
Step1: 下載OpenCV
我選最新的 4.0.0-beta 3.4.3 下載
檔案是一個exe 點兩下解壓縮
Step2: 安裝OpenCV
OpenCV 2.4.6 + visual studio 2012 安裝教學
遇到此錯誤 cannot open input file 'opencv_calib3d246.lib
查到這篇文 Installing OpenCV 2.4.3 in Visual C++ 2010 Express [closed]
注意裡面這句話
Note that the filenames end with "d" (for "debug"). Also note that if you have installed another version of OpenCV (say 2.4.9) these filenames will end with 249d instead of 243d (opencv_core249d.lib..etc).
所以我將 Linker → Input → Additional Dependencies 的entry都修改成xxxxx400.lib發現也無效 (這時我下載的OpenCV版本是4.0.0 beta)
改下載 3.4.3 也有一樣的error
喵的美送! 直接拿掉 opencv_calib3d343.lib這個entry! >shit 還是有類似的error 我全拿總可以了吧!(失去理智)->error變更多
算了 重找教學 !!!!!!! 找到底下這個終於成功了
OpenCV 3.4.1 安裝配置在 Visual Studio 2017
看起來是因為原本我找的教學 OpenCV跟VS版本都太舊的關係
已經跟不上潮流
利用OpenCV處理IR camera影像
程式邏輯我是參考底下連結的Step9:
HOW TO MAKE LASER PROJECTION VIRTUAL KEYBOARD
我將Step9截圖如下
我手上已經有IR camera的影像,所以上圖的Step1:Camera get object image不用作
我只要做Step2 & 3 即可
程式碼片段
以下程式碼我跑過沒有問題
若是那種程式碼看都沒看直接複製貼上
(ex: 需修改成自己電腦環境的 比如圖檔路徑)
然後跑不起來的智障問題恕我無法回答
#include "stdafx.h"
#include "IRcamera.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
//IR camera image
Mat src = imread("C:\\Users\\Gary\\Desktop\\IR.jpg");
//Grayscale matrix
Mat grayscaleMat(src.size(), CV_8U);
//Convert BGR to Gray
cvtColor(src, grayscaleMat, CV_BGR2GRAY);
//Binary image
Mat binaryMat(grayscaleMat.size(), grayscaleMat.type());
//Apply thresholding
threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);
//Show the results
//namedWindow("Output", cv::WINDOW_AUTOSIZE);
//imshow("Output", binaryMat);
//Extract the contours so that
vector<vector<Point> > contours0;
findContours(binaryMat, contours0, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
Mat dst = Mat::zeros(binaryMat.rows, binaryMat.cols, CV_8UC3);
// Draw the object contour
int idx = 0;
for (; idx >= 0; idx = hierarchy[idx][0])// iterate through all the top-level contours,
{
//Scalar color(rand() & 255, rand() & 255, rand() & 255); // draw each connected component with its own random color
Scalar white(255, 255, 255);
drawContours(dst, contours0, idx, white, FILLED, 8, hierarchy);
}
// Draw the object contour of the green(0, 255, 0) rectangle
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;
for (int i = 0; i < contours0.size(); i++) // Iterate through each contour
{
double a = contourArea(contours0[i], false); // Find the area of contour
bounding_rect = boundingRect(contours0[i]);
rectangle(dst, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
//Bounding Box Centroid
Point center = (bounding_rect.br() + bounding_rect.tl())*0.5;
Scalar red(0, 0, 255);
circle(dst, center, 2, red, CV_FILLED, 8, 0);
}
namedWindow("Components", 1);
imshow("Components", dst);
// Wait for a keystroke in the window
waitKey();
}
結果
input
output
參考資料
Structural Analysis and Shape Descriptors 這邊可以查到OpenCV函式的定義,有的還有範例
留言列表