爲此,您可以找到連接組件的邊框。
要查找連接的組件,可以將其轉換爲灰度,並保留所有像素值爲0的矩形,即黑色邊框。
然後你就可以找到每個連接部件的輪廓,並計算其邊框。在這裏發現了紅色邊框:
代碼:
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
// Load the image, as BGR
Mat3b img = imread("path_to_image");
// Convert to gray scale
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// Get binary mask
Mat1b binary = (gray == 0);
// Find contours of connected components
vector<vector<Point>> contours;
findContours(binary.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// For each contour
for (int i = 0; i < contours.size(); ++i)
{
// Get the bounding box
Rect box = boundingRect(contours[i]);
// Draw the box on the original image in red
rectangle(img, box, Scalar(0, 0, 255), 5);
}
// Show result
imshow("Result", img);
waitKey();
return 0;
}
從張貼在聊天的圖像,此代碼生成:
一般,這段代碼會正確檢測卡片,以及噪音。你只需要根據一些標準去除噪音。其中包括:框的大小或縱橫比,框內的顏色以及一些紋理信息。
難道你不能簡單地計算連接組件的邊界框嗎? – Miki
@Miki我是新來的對象識別。你能指出我對你的推薦算法的正確方向嗎? – RobertJoseph
你用C++編程嗎? – Miki