CPP
#include <iostream>
#include <stdio.h>
#include <opencv.hpp>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <omp.h>
using namespace cv;
using namespace std;
extern "C" void Binary_Image_CUDA(unsigned char* d_in, unsigned char* d_out, int imageHeight, int imageWidth, int threadDown, int threadUP);
Mat convertTo3Channels(const Mat& binImg)
{
Mat three_channel = Mat::zeros(binImg.rows, binImg.cols, CV_8UC3);
vector<Mat> channels;
for (int i = 0; i < 3; i++)
{
channels.push_back(binImg);
}
merge(channels, three_channel);
return three_channel;
}
int main() {
//对图像进行距离变换
Mat img = imread("1.jpg");
if (img.empty())
{
cout << "请确认输入的图片路径是否正确" << endl;
return -1;
}
if (img.channels()==3)
{
cvtColor(img, img, COLOR_BGR2GRAY);
}
//裁剪
Rect rect(0, 13580, 11000, 27020);
Mat ROI = img(rect);
Mat image1;
ROI.copyTo(image1);
//二值化
int imgHeight = image1.rows;
int imgWidth = image1.cols;
int length = imgHeight * imgWidth;
unsigned char* d_in;
unsigned char* d_out;
cv::Mat dst(imgHeight, imgWidth, CV_8UC1);
cudaMalloc((void**)&d_in, imgHeight * imgWidth * sizeof(unsigned char));
cudaMalloc((void**)&d_out, imgHeight * imgWidth * sizeof(unsigned char));
cudaMemcpy(d_in, image1.data, imgHeight * imgWidth * sizeof(unsigned char), cudaMemcpyHostToDevice);
auto starttime = std::chrono::system_clock::now();
Binary_Image_CUDA(d_in, d_out, imgHeight, imgWidth, 0, 240);
std::chrono::duration<double> diff = std::chrono::system_clock::now() - starttime;
cudaMemcpy(dst.data, d_out, imgHeight * imgWidth * sizeof(unsigned char), cudaMemcpyDeviceToHost);
cout << "CUDA耗时:" << diff.count() * 1000 << "ms" << endl;
//开运算
cv::Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
cv::morphologyEx(dst, dst, MORPH_OPEN, element);
//找轮廓
vector< vector<Point> > contours;
vector<Vec4i> hireachy;
findContours(dst, contours, hireachy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat res = convertTo3Channels(ROI);
RNG rng(10086);
//判断NG 并绘制NG芯片的最小外接矩形框
omp_set_num_threads(20);
#pragma omp parallel for
for (int t = 0; t < contours.size(); t++)
{
if (contours[t].size()<100)
{
continue;
}
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
Rect rect = boundingRect(contours[t]);
Mat testImg = image1(rect);
Scalar fScalarMean = mean(testImg, testImg);
double meanValue = fScalarMean[0]; //平均灰度
int width = testImg.cols; //宽
int height = testImg.rows; //高
//判断条件
bool bMeanValue = (meanValue > 150) ? true : false;
bool bWidth = (width > 70) ? true : false;
bool bHeight = (height > 70) ? true : false;
if (bMeanValue || bWidth || bHeight)
{
rectangle(res, rect, color, 1);
}
}
system("pause");
return 0;
}
.cu
#include <iostream>
#include <opencv.hpp>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void THRESH_BINARY_CUDA(unsigned char* d_in, unsigned char* d_out, int imageHeight, int imageWidth, int threadDown, int threadUP)
{
int xIndex = threadIdx.x + blockIdx.x * blockDim.x;
int yIndex = threadIdx.y + blockIdx.y * blockDim.y;
int index = yIndex * imageWidth + xIndex;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x;
if (xIndex < imageWidth && yIndex < imageHeight)
{
d_out[index] = (d_in[index] >= threadDown && d_in[index] <= threadUP) ? 255 : 0;
}
}
extern "C" void Binary_Image_CUDA(unsigned char* d_in, unsigned char* d_out, int imageHeight, int imageWidth, int threadDown, int threadUP)
{
dim3 block(32, 32);
dim3 grid((imageWidth + block.x - 1) / block.x, (imageHeight + block.y - 1) / block.y);
THRESH_BINARY_CUDA << <grid, block >> > (d_in, d_out, imageHeight, imageWidth, threadDown, threadUP);
cudaThreadSynchronize();
}