原图:
首先想到的是基于边缘检测或者阈值分割的方法进行检测:
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat org, dst, img, tmp;
void on_mouse(int event, int x, int y, int flags, void *)
{
static Point pre_pt = (-1, -1);
static Point cur_pt = (-1, -1);
if (event == CV_EVENT_LBUTTONDOWN) {
pre_pt = Point(x, y);
}
else if (event == CV_EVENT_MOUSEMOVE && flags)//摁下左键,flags为1
{
org.copyTo(tmp);
cur_pt = Point(x, y);
rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", tmp);//画的时候显示框
}else if (event == CV_EVENT_LBUTTONUP) {
org.copyTo(img);
//cur_pt = Point(x, y);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", img);//画完后显示框
//img.copyTo(tmp);
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
namedWindow("dst");
imshow("dst", dst);
}
}
void main() {
org = imread("123.jpg");
org.copyTo(img);
namedWindow("img");
setMouseCallback("img", on_mouse, 0);
imshow("img", img);
waitKey();
Mat src_gray;
cvtColor(dst, src_gray, CV_WINDOW_AUTOSIZE);
Mat src_canny;
Canny(src_gray, src_canny, 120, 200);
//threshold(src_gray, src_canny, 60, 255, THRESH_OTSU);
//边缘检测
imshow("edge img", src_canny);
waitKey();
//霍夫直线检测
vector<Vec4f> line_data;
HoughLinesP(src_canny, line_data, 1, CV_PI / 180.0, 80, 10, 20); //把得到的直线显示在图中
Scalar color = Scalar(255, 0, 0);
for (size_t i = 0; i < line_data.size(); i++) {
Vec4f temp = line_data[i];
line(dst, Point(temp[0], temp[1]), Point(temp[2], temp[3]), color, 2);
}
imshow("houghLinesP img", dst);
waitKey(0);
}
效果差强人意,待优化:
第二种方法:根据边缘灰度分布,我们要了解,每个分割线一定会有一个有0-255-0的灰度变换,每隔灰度变化代表一层,根据这个原理进行自上而下遍历查找灰度变化会准确很多。
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 221;
int max_thresh = 255;
RNG rng(12345);
int inti = 20;
int max_inti = 200;
/// Function header
void thresh_callback(int, void*);
/** @function main */
int main(int argc, char** argv)
{
src = imread("123捕获.PNG");
cvtColor(src, src_gray, CV_BGR2GRAY);
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src_gray);
createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
createTrackbar(" Thresholdcols:", "Source", &inti, max_inti, thresh_callback);
thresh_callback(0, 0);
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat threshold_output;
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
//filter2D(src_gray, src_gray, CV_8UC3, kernel);
equalizeHist(src_gray, threshold_output);
//resize(src_gray, src_gray, Size(512, 300),INTER_LINEAR);
threshold(threshold_output, threshold_output, thresh, 255, THRESH_BINARY);
int b = 0;
for (int i = 1;i < threshold_output.rows;i++) {
if ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i + 1, inti) < 10) || ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i - 1, inti) < 10))) {
b++;
}
}
cout << (b / 2) + 1 << endl;
/// 结果在窗体中显示
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", threshold_output);
}
代码二:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 221;
int max_thresh = 255;
RNG rng(12345);
int inti = 20;
int max_inti = 200;
/// Function header
void thresh_callback(int, void*);
void thresh_callback1(int, void*);
/** @function main */
int main(int argc, char** argv)
{
src = imread("123捕获.PNG");
cvtColor(src, src_gray, CV_BGR2GRAY);
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src_gray);
createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
thresh_callback(0, 0);
createTrackbar(" Thresholdcols:", "Source", &inti, max_inti, thresh_callback1);
thresh_callback1(0, 0);
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat threshold_output;
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
//filter2D(src_gray, src_gray, CV_8UC3, kernel);
equalizeHist(src_gray, threshold_output);
//resize(src_gray, src_gray, Size(512, 300),INTER_LINEAR);
threshold(threshold_output, threshold_output, thresh, 255, THRESH_BINARY);
int b = 0;
for (int i = 1;i < threshold_output.rows;i++) {
if ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i + 1, inti) < 10) || ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i - 1, inti) < 10))) {
b++;
}
}
cout << (b / 2) + 1 << endl;
/// 结果在窗体中显示
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", threshold_output);
}
void thresh_callback1(int, void*)
{
Mat threshold_output;
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
//filter2D(src_gray, src_gray, CV_8UC3, kernel);
equalizeHist(src_gray, threshold_output);
//resize(src_gray, src_gray, Size(512, 300),INTER_LINEAR);
threshold(threshold_output, threshold_output, thresh, 255, THRESH_BINARY);
int b = 0;
for (int i = 1;i < threshold_output.rows;i++) {
if ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i + 1, inti) < 10) || ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i - 1, inti) < 10))) {
b++;
}
}
cout << (b / 2) + 1 << endl;
/// 结果在窗体中显示
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", threshold_output);
}
代码三:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 221;
int max_thresh = 255;
RNG rng(12345);
int inti = 20;
int max_inti = 200;
/// Function header
/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat threshold_output;
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
//filter2D(src_gray, src_gray, CV_8UC3, kernel);
equalizeHist(src_gray, threshold_output);
//resize(src_gray, src_gray, Size(512, 300),INTER_LINEAR);
threshold(threshold_output, threshold_output, thresh, 255, THRESH_BINARY);
int b = 0;
for (int i = 1;i < threshold_output.rows;i++) {
if ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i + 1, inti) < 10) || ((threshold_output.at<uchar>(i, inti) == 255 && threshold_output.at<uchar>(i - 1, inti) < 10))) {
b++;
}
}
cout << (b / 2) + 1 << endl;
/// 结果在窗体中显示
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", threshold_output);
}
Mat org, dst, img, tmp;
void on_mouse(int event, int x, int y, int flags, void *)
{
static Point pre_pt = (-1, -1);
static Point cur_pt = (-1, -1);
if (event == CV_EVENT_LBUTTONDOWN)
{
pre_pt = Point(x, y);
}
else if (event == CV_EVENT_MOUSEMOVE && flags)//摁下左键,flags为1
{
org.copyTo(tmp);
cur_pt = Point(x, y);
rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", tmp);//画的时候显示框
}
else if (event == CV_EVENT_LBUTTONUP)
{
org.copyTo(img);
//cur_pt = Point(x, y);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", img);//画完后显示框
//img.copyTo(tmp);
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
namedWindow("dst");
imshow("dst", dst);
}
}
void main(){
org = imread("123捕获.PNG");
org.copyTo(img);
namedWindow("img");
imshow("img", img);
setMouseCallback("img", on_mouse, 0);
waitKey();
cvtColor(dst, src_gray, CV_BGR2GRAY);
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src_gray);
createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
createTrackbar(" Thresholdcols:", "Source", &inti, max_inti, thresh_callback);
thresh_callback(0, 0);
waitKey(0);
}
第三种算法:钢板厚度相同,每片所占的像素数量相同,计算单个像素厚度,在计算总数目。