Tile detection algorithm using opencv


After the success of running code on Matlab, we found it necessary to move the algorithm to other platform. Using opencv, which is a multi-platform library, it is easy to adapt the algorithm into embedded systems.

Opencv is very friendly and powerful, we soon learned the basic usage and convert the original code to opencv.

Sample pictures:

blob.png

Using canny

blob.png

Complete contours are detected

Code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_modules.hpp>

using namespace cv;
using namespace std;

/**************Definitons*************/
#define MAXRECT				100
#define ID				"50-0503334900"
#define THRESH				100
#define AREALIMIT			1000.0
/**************Variables**************/
vector<RotatedRect>	rectProfile;
vector<vector<Point>>	contoursRect;
vector<vector<Point>>	contours;

char buf[BUF_SIZE];

/**************Functions**************/
bool Check(vector<Point> con){
	//printf("%d\n", con.size());
	double area = abs(contourArea(con, 1));
	printf("%lf\n", area);
	if (area < AREALIMIT) return false;
	return true;
	RotatedRect profile = minAreaRect(con);
	double len = profile.size.height;
	double wid = profile.size.width;
	if (len < wid) swap(len, wid);
	if (len / wid > 4.0)
		return false;
	if (len / wid < 1.5)
		return false;
	return true;
}

void ImageProcess(){
	//ImageMaker();
	//Mat img = Mat(height, width, CV_8UC1, imgBuffer);
	Mat img = imread("sample00012_5.BMP", CV_LOAD_IMAGE_GRAYSCALE);//total

	Mat imgCanny;
	Canny(img, imgCanny, THRESH, THRESH * 2);
	imshow("Image2", imgCanny);
	imwrite("ImageCanny.jpg",imgCanny);
	waitKey();

	findContours(imgCanny, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
	
	Mat cc = Mat::zeros(imgCanny.rows, imgCanny.cols, CV_8UC3);
	for (int j = 0; j < contours.size(); j++){
		Scalar color(rand()&255, rand()&255, rand()&255);
		drawContours(cc, contours, j, color);
	}
	imshow("Results_1", cc);
	imwrite("Results_1.jpg",cc);
	int n = 0;
	contoursRect.clear();
	for (unsigned int i = 0; i < contours.size(); i++){
		if (Check(contours[i])){
			contoursRect.push_back(contours[i]);
			n++;
		}
	}
	printf("%d\n", n);

	Mat ccc = Mat::zeros(imgCanny.rows, imgCanny.cols, CV_8UC3);
	for (int i = 0; i < n; i++){
		Scalar color(rand()&255, rand()&255, rand()&255);
		drawContours(ccc, contoursRect, i, color);
	}
	imshow("Results", ccc);
	imwrite("Results_2.jpg",ccc);
	waitKey();
	destroyAllWindows();
	rectProfile.clear();
	for (int i = 0; i < n; i++) {
		rectProfile.push_back(minAreaRect(contoursRect[i]));
	}
}


int main(){
	contours.reserve(100000*sizeof(Point));
	ImageProcess();
}


This article is among the series of FDUROP project. More information: http://104.131.150.53/thinkcmfx/index.php?g=&m=article&a=index&id=17


Last Article Next article

Comment 评论



Share 分享

New Users 最新加入

  • hokurikustr

  • refrain

New comments 最新评论

test123: aasdas Details Apr 13 16:39
admin: Thanks! Details Apr 09 11:46
admin: Google map api Details Apr 09 11:46
lqj12: cooooooooool Details Apr 08 21:34
Yunhan Huang: 这个功能是如何实现的? Details Apr 08 13:23