2015-12-28 115 views
0

中的對象的位置(S)我有一個看起來像這樣的輸入圖像:檢測圖像

input image

注意到有6盒與黑色邊框。我需要檢測每個盒子的位置(左上角)。通常我會使用類似template matching的東西,但每個盒子的內容(黑色邊框內的彩色區域)是不同的。

是否有可配置爲忽略每個盒子內部區域的模板匹配版本?該算法是否更適合這種情況?

另外請注意,我必須處理幾種不同的分辨率......因此,這些盒子的實際尺寸會因圖像而異。也就是說,比例(長寬比)總是相同的。

真實世界的每個請求的例子/輸入圖像:

input image

+1

難道你不能簡單地計算連接組件的邊界框嗎? – Miki

+0

@Miki我是新來的對象識別。你能指出我對你的推薦算法的正確方向嗎? – RobertJoseph

+0

你用C++編程嗎? – Miki

回答

5

爲此,您可以找到連接組件的邊框。

要查找連接的組件,可以將其轉換爲灰度,並保留所有像素值爲0的矩形,即黑色邊框。

enter image description here

然後你就可以找到每個連接部件的輪廓,並計算其邊框。在這裏發現了紅色邊框:

enter image description here

代碼:

#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; 
} 

從張貼在聊天的圖像,此代碼生成:

enter image description here

一般,這段代碼會正確檢測卡片,以及噪音。你只需要根據一些標準去除噪音。其中包括:框的大小或縱橫比,框內的顏色以及一些紋理信息。