【无标题】

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();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值