赞
踩
CVPublic.h
#ifndef CVPUBLIC_H #define CVPUBLIC_H /********************************************************************** *@projectName Opencv Public *@brief 摘要 * 1.Qt使用OpenCV 图像处理一些常用方法 *Author: zlozl *Copyright: zlozl *Email: zlozl5566@163.com *QQ: 849414798 *BlogAddress: https://blog.csdn.net/ZLOZL *GiteeHome: https://gitee.com/zlozl5566 *Version: V1.0.0.1 *Date: 2020-11-08 **********************************************************************/ #include <QObject> #include <QMutex> #include <QDebug> #include <QImage> #include <opencv2/opencv.hpp> class CVPublic { public: CVPublic(); ~CVPublic(); static CVPublic *Instance(); /* * @brief * 图像转化 * * * @param * * @return */ // Mat 改成 QImage QImage cvMatToQImage(const cv::Mat &mat); QImage cvMatToQImage2(const cv::Mat &mat); QImage cvMatToQImage3(const cv::Mat &mat); // QImage 改成 Mat cv::Mat QImageTocvMat2(QImage image); // 提取RGB分量 int spliteRGB(QImage insrc, int color, QImage *outsrc); // 提取分量 model 为RGB 和非RGB int spliteColor(QImage insrc, cv::String model, int color, QImage *outsrc); /* * @brief * 图像几何变换 * * * @param * * @return */ void Resize(QImage &src, int length, int width);// 改变大小 // 谷歌党用如下声明函数 // void Resize(const QImage &insrc, int length, int width, QImage *outsrc); void Enlarge_Reduce(QImage &src, int times);// 缩放 void Rotate(QImage &src, int angle);// 旋转 void Rotate_fixed(QImage &src, int angle);// 旋转90,180,270 void Flip(QImage &src, int flipcode);// 镜像 void Lean(QImage &src, int x, int y);// 倾斜 /* * @brief * 图像增强 * * * @param * * @return */ QImage Normalized(QImage src, int kernel_length);// 简单滤波 QImage Gaussian(QImage src, int kernel_length); // 高斯滤波 QImage Median(QImage src, int kernel_length);// 中值滤波 QImage Sobel(QImage src, int kernel_length);// sobel边缘检测 QImage Laplacian(QImage src, int kernel_length);// laplacian边缘检测 QImage Canny(QImage src, int kernel_length, int lowThreshold);// canny边缘检测 QImage HoughLine(QImage src, int threshold, double minLineLength, double maxLineGap);// 线检测 QImage HoughCircle(QImage src, int minRadius, int maxRadius);// 圆检测 /* * @brief * 灰度变化 * * * @param * * @return */ QImage Bin(QImage src, int threshold); QImage Graylevel(QImage src); QImage Reverse(QImage src); // 图像反转 QImage Linear(QImage src, int alpha, int beta); // 线性变换 QImage Gamma(QImage src, int gamma); // 伽马变换(指数变换) QImage Log(QImage src, int c); // 对数变换 QImage Histeq(QImage src); // 直方图均衡化 void BinToGraylevel(cv::Mat &image); bool IsBin(cv::Mat &image); /* * @brief * 图像腐蚀 * * * @param * * @return */ QImage Erode(QImage src, int elem, int kernel, int times);// 腐蚀 QImage Dilate(QImage src, int elem, int kernel, int times);// 膨胀 QImage Open(QImage src, int elem, int kernel, int times);// 开运算 QImage Close(QImage src, int elem, int kernel, int times);// 闭运算 QImage Grad(QImage src, int elem, int kernel);// 形态学梯度 QImage Tophat(QImage src, int elem, int kernel);// 顶帽操作 QImage Blackhat(QImage src, int elem, int kernel);// 黑帽操作 private: static QScopedPointer<CVPublic> self; }; #endif // CVPUBLIC_H
CVPublic.cpp
#include "cvpublic.h" using namespace cv; using namespace std; CVPublic::CVPublic() { qDebug()<< "CVPublic is Created"; } CVPublic::~CVPublic() { qDebug()<< "CVPublic is Destoryed"; } QScopedPointer<CVPublic> CVPublic::self; CVPublic *CVPublic::Instance() { if (self.isNull()) { static QMutex mutex; QMutexLocker locker (&mutex); if (self.isNull()) { self.reset(new CVPublic); } } return self.data(); } QImage CVPublic::cvMatToQImage(const Mat &mat) { cv::Mat rgbFrame; cv::cvtColor(mat,rgbFrame, COLOR_BGR2RGB); QImage img((const uchar*)rgbFrame.data,rgbFrame.cols,rgbFrame.rows,rgbFrame.step,QImage::Format_RGB888); img.bits(); //深拷贝,避免线程退出内存错误 return img; } QImage CVPublic::cvMatToQImage2(const Mat &mat) { if (mat.type() == CV_8UC1) { // 单通道 QImage image(mat.cols, mat.rows, QImage::Format_Indexed8); image.setColorCount(256); // 灰度级数256 for (int i = 0; i < 256; i++) { image.setColor(i, qRgb(i, i, i)); } uchar *pSrc = mat.data; // 复制mat数据 for (int row = 0; row < mat.rows; row++) { uchar *pDest = image.scanLine(row); memcpy(pDest, pSrc, static_cast<unsigned long>(mat.cols)); pSrc += static_cast<long>(mat.step); } return image; } else if (mat.type() == CV_8UC3) { // 3通道 const uchar *pSrc = const_cast<const uchar *>(mat.data); // 复制像素 QImage image(pSrc, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_RGB888); // R, G, B 对应 0,1,2 return image.rgbSwapped(); // rgbSwapped是为了显示效果色彩好一些。 } else if (mat.type() == CV_8UC4) { const uchar *pSrc = const_cast<const uchar *>(mat.data); // 复制像素 // Create QImage with same dimensions as input Mat QImage image(pSrc, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_ARGB32); // B,G,R,A 对应 0,1,2,3 return image.copy(); } else { return QImage(); } } QImage CVPublic::cvMatToQImage3(const Mat &mat) { cv::Mat TmpMat; // convert the color space to RGB if (mat.channels() == 1) { cv::cvtColor(mat, TmpMat, COLOR_GRAY2RGB); } else { cv::cvtColor(mat, TmpMat, COLOR_BGR2RGB); } // construct the QImage using the data of the mat, while do not copy the data QImage Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows, QImage::Format_RGB888); // deep copy the data from mat to QImage Result.bits(); return Result; } Mat CVPublic::QImageTocvMat2(QImage image) { Mat mat; switch (image.format()) { case QImage::Format_ARGB32: case QImage::Format_RGB32: case QImage::Format_ARGB32_Premultiplied: mat = Mat(image.height(), image.width(), CV_8UC4, (void *)image.constBits(), static_cast<unsigned long>( image.bytesPerLine())); break; case QImage::Format_RGB888: mat = Mat(image.height(), image.width(), CV_8UC3, (void *)image.constBits(), static_cast<unsigned long>( image.bytesPerLine())); cv::cvtColor(mat, mat, COLOR_BGR2RGB); break; case QImage::Format_Indexed8: case QImage::Format_Grayscale8: mat = Mat(image.height(), image.width(), CV_8UC1, (void *)image.constBits(), static_cast<unsigned long>( image.bytesPerLine())); break; } return mat; } int CVPublic::spliteRGB(QImage insrc, int color, QImage *outsrc) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(insrc); if (srcImg.channels() == 1) { qDebug()<< "该图像为灰度图像。"; return -1; } else { vector<Mat> m; split(srcImg, m); vector<Mat>Rchannels, Gchannels, Bchannels; split(srcImg, Rchannels); split(srcImg, Gchannels); split(srcImg, Bchannels); Rchannels[1] = 0; Rchannels[2] = 0; Gchannels[0] = 0; Gchannels[2] = 0; Bchannels[0] = 0; Bchannels[1] = 0; merge(Rchannels, m[0]); merge(Gchannels, m[1]); merge(Bchannels, m[2]); dstImg = m[static_cast<unsigned long>(color)]; // 分别对应B、G、R QImage dst = cvMatToQImage2(dstImg); *outsrc = dst; return 1; } } int CVPublic::spliteColor(QImage insrc, String model, int color, QImage *outsrc) { if (model == "RGB") { return spliteRGB(insrc,color,outsrc); } else { Mat img = QImageTocvMat2(insrc); Mat img_rgb, img_hsv, img_hls, img_yuv, img_dst; if (img.channels() == 1) { qDebug()<< "该图像为灰度图像。"; return -1; } else { vector <Mat> vecRGB, vecHsv, vecHls, vecYuv; img_hsv.create(img.rows, img.cols, CV_8UC3); img_hls.create(img.rows, img.cols, CV_8UC3); cvtColor(img, img_rgb, COLOR_BGR2RGB); cvtColor(img, img_hsv, COLOR_BGR2HSV); cvtColor(img, img_hls, COLOR_BGR2HLS); cvtColor(img, img_yuv, COLOR_BGR2YUV); split(img_rgb, vecRGB); split(img_hsv, vecHsv); split(img_hls, vecHls); split(img_yuv, vecYuv); if (model == "RGB") { img_dst = vecRGB[static_cast<unsigned long>(color)]; } else if (model == "HSV") { img_dst = vecHsv[static_cast<unsigned long>(color)]; } else if (model == "HLS") { img_dst = vecHls[static_cast<unsigned long>(color)]; } else if (model == "YUV") { img_dst = vecYuv[static_cast<unsigned long>(color)]; } else { img_dst = img; } QImage dst = cvMatToQImage2(img_dst); *outsrc = dst; return 1; } } } void CVPublic::Resize(QImage &src, int length, int width) { Mat matSrc, matDst; matSrc = QImageTocvMat2(src); resize(matSrc, matDst, Size(length, width), 0, 0, INTER_LINEAR);// 线性插值 src = cvMatToQImage2(matDst); } void CVPublic::Enlarge_Reduce(QImage &src, int times) { Mat matSrc, matDst; matSrc = QImageTocvMat2(src); if (times > 0) { resize(matSrc, matDst, Size(matSrc.cols * abs(times + 1), matSrc.rows * abs(times + 1)) , 0, 0, INTER_LINEAR); src = cvMatToQImage2(matDst); return ; } else if (times < 0) { resize(matSrc, matDst, Size(matSrc.cols / abs(times - 1), matSrc.rows / abs(times - 1)) , 0, 0, INTER_AREA); src = cvMatToQImage2(matDst); return ; } else { return ; } } void CVPublic::Rotate(QImage &src, int angle) { Mat matSrc, matDst, M; matSrc = QImageTocvMat2(src); cv::Point2f center(matSrc.cols / 2, matSrc.rows / 2); cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1); cv::Rect bbox = cv::RotatedRect(center, matSrc.size(), angle).boundingRect(); ; rot.at<double>(0, 2) += bbox.width / 2.0 - static_cast<double>(center.x); rot.at<double>(1, 2) += bbox.height / 2.0 - static_cast<double>(center.y); cv::warpAffine(matSrc, matDst, rot, bbox.size()); src = cvMatToQImage2(matDst); return ; } void CVPublic::Rotate_fixed(QImage &src, int angle) { Mat matSrc, matDst, M; matSrc = QImageTocvMat2(src); M = getRotationMatrix2D(Point2i(matSrc.cols / 2, matSrc.rows / 2), angle, 1); warpAffine(matSrc, matDst, M, Size(matSrc.cols, matSrc.rows)); src = cvMatToQImage2(matDst); return ; } void CVPublic::Flip(QImage &src, int flipcode) { Mat matSrc, matDst; matSrc = QImageTocvMat2(src); flip(matSrc, matDst, flipcode); // flipCode==0 垂直翻转(沿X轴翻转),flipCode>0 水平翻转(沿Y轴翻转) // flipCode<0 水平垂直翻转(先沿X轴翻转,再沿Y轴翻转,等价于旋转180°) src = cvMatToQImage2(matDst); return ; } void CVPublic::Lean(QImage &src, int x, int y) { Mat matSrc, matTmp, matDst; matSrc = QImageTocvMat2(src); matTmp = Mat::zeros(matSrc.rows, matSrc.cols, matSrc.type()); Mat map_x, map_y; Point2f src_point[3], tmp_point[3], x_point[3], y_point[3]; double angleX = x / 180.0 * CV_PI ; double angleY = y / 180.0 * CV_PI; src_point[0] = Point2f(0, 0); src_point[1] = Point2f(matSrc.cols, 0); src_point[2] = Point2f(0, matSrc.rows); x_point[0] = Point2f(static_cast<float>(matSrc.rows) * static_cast<float>(tan(angleX)), 0); x_point[1] = Point2f(static_cast<int>(matSrc.cols + matSrc.rows * tan(angleX)), 0); x_point[2] = Point2f(0, matSrc.rows); map_x = getAffineTransform(src_point, x_point); warpAffine(matSrc, matTmp, map_x, Size(static_cast<int>(matSrc.cols + matSrc.rows * tan(angleX)), matSrc.rows)); tmp_point[0] = Point2f(0, 0); tmp_point[1] = Point2f(matTmp.cols, 0); tmp_point[2] = Point2f(0, matTmp.rows); y_point[0] = Point2f(0, 0); y_point[1] = Point2f(matTmp.cols, static_cast<float>(matTmp.cols * tan(angleY))); y_point[2] = Point2f(0, matTmp.rows); map_y = getAffineTransform(tmp_point, y_point); warpAffine(matTmp, matDst, map_y, Size(matTmp.cols, static_cast<int> (matTmp.rows + matTmp.cols * tan(angleY)))); src = cvMatToQImage2(matDst); return ; } QImage CVPublic::Normalized(QImage src, int kernel_length) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); blur(srcImg, dstImg, Size(kernel_length, kernel_length), Point(-1, -1)); return cvMatToQImage2(dstImg); } QImage CVPublic::Gaussian(QImage src, int kernel_length) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); GaussianBlur(srcImg, dstImg, Size(kernel_length, kernel_length), 0, 0); return cvMatToQImage2(dstImg); } QImage CVPublic::Median(QImage src, int kernel_length) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); medianBlur(srcImg, dstImg, kernel_length); return cvMatToQImage2(dstImg); } QImage CVPublic::Sobel(QImage src, int kernel_length) { Mat srcImg, dstImg, src_gray; srcImg = QImageTocvMat2(src); GaussianBlur(srcImg, srcImg, Size(3, 3), 0, 0, BORDER_DEFAULT); // 高斯模糊 if (srcImg.channels() != 1) { cvtColor(srcImg, src_gray, COLOR_BGR2GRAY); // 转换灰度图像 } else { src_gray = srcImg; } Mat grad_x, grad_y, abs_grad_x, abs_grad_y; cv::Sobel(src_gray, grad_x, CV_16S, 1, 0, kernel_length, 1, 0, BORDER_DEFAULT); cv::Sobel(src_gray, grad_y, CV_16S, 0, 1, kernel_length, 1, 0, BORDER_DEFAULT); convertScaleAbs(grad_x, abs_grad_x); // 缩放,计算绝对值,并将结果转换为8位 convertScaleAbs(grad_y, abs_grad_y); addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dstImg); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Laplacian(QImage src, int kernel_length) { Mat srcImg, dstImg, src_gray; srcImg = QImageTocvMat2(src); GaussianBlur(srcImg, srcImg, Size(3, 3), 0, 0, BORDER_DEFAULT); // 高斯模糊 if (srcImg.channels() != 1) { cvtColor(srcImg, src_gray, COLOR_BGR2GRAY); // 转换灰度图像 } else { src_gray = srcImg; } Mat abs_dst; // 拉普拉斯二阶导数 cv::Laplacian(src_gray, dstImg, CV_16S, kernel_length, 1, 0, BORDER_DEFAULT); convertScaleAbs(dstImg, dstImg); // 绝对值8位 QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Canny(QImage src, int kernel_length, int lowThreshold) { Mat srcImg, dstImg, src_gray, detected_edges; srcImg = QImageTocvMat2(src); dstImg.create(srcImg.size(), srcImg.type()); if (srcImg.channels() != 1) { cvtColor(srcImg, src_gray, COLOR_BGR2GRAY); // 转换灰度图像 } else { src_gray = srcImg; } blur(src_gray, detected_edges, Size(3, 3)); // 平均滤波平滑 cv::Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * 3, kernel_length); dstImg = Scalar::all(0); srcImg.copyTo(dstImg, detected_edges); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::HoughLine(QImage src, int threshold, double minLineLength, double maxLineGap) { Mat srcImg, dstImg, cdstPImg; srcImg = QImageTocvMat2(src); cv::Canny(srcImg, dstImg, 50, 200, 3); // Canny算子边缘检测 if (srcImg.channels() != 1) { cvtColor(dstImg, cdstPImg, COLOR_GRAY2BGR); // 转换灰度图像 } else { cdstPImg = srcImg; } vector<Vec4i> linesP; HoughLinesP(dstImg, linesP, 1, CV_PI / 180, threshold, minLineLength, maxLineGap); // 50,50,10 for (size_t i = 0; i < linesP.size(); i++) { Vec4i l = linesP[i]; line(cdstPImg, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 1, LINE_AA); } return cvMatToQImage2(cdstPImg); } QImage CVPublic::HoughCircle(QImage src, int minRadius, int maxRadius) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); Mat gray; if (srcImg.channels() != 1) { cvtColor(srcImg, gray, COLOR_BGR2GRAY); } else { gray = srcImg; } medianBlur(gray, gray, 5); // 中值滤波,滤除噪声,避免错误检测 vector<Vec3f> circles; HoughCircles(gray, circles, HOUGH_GRADIENT, 1, gray.rows / 16, 100, 30, minRadius, maxRadius); // Hough圆检测,100, 30, 1, 30 dstImg = srcImg.clone(); for (size_t i = 0; i < circles.size(); i++) { Vec3i c = circles[i]; Point center = Point(c[0], c[1]); circle(dstImg, center, 1, Scalar(0, 100, 100), 3, LINE_AA); // 画圆 int radius = c[2]; circle(dstImg, center, radius, Scalar(255, 0, 255), 3, LINE_AA); } QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Bin(QImage src, int threshold) { Mat srcImg, dstImg, grayImg; srcImg = QImageTocvMat2(src); if (srcImg.channels() != 1) { cvtColor(srcImg, grayImg, COLOR_BGR2GRAY); } else { dstImg = srcImg.clone(); } cv::threshold(grayImg, dstImg, threshold, 255, THRESH_BINARY); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Graylevel(QImage src) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); dstImg.create(srcImg.size(), srcImg.type()); if (srcImg.channels() != 1) { cvtColor(srcImg, dstImg, COLOR_BGR2GRAY); } else { dstImg = srcImg.clone(); } if (IsBin(dstImg)) { BinToGraylevel(dstImg); } QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Reverse(QImage src) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); bitwise_xor(srcImg, Scalar(255), dstImg); // 异或 QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Linear(QImage src, int alpha, int beta) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); srcImg.convertTo(dstImg, -1, alpha / 100.0, beta - 100); // matDst = alpha * matTmp + beta QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Gamma(QImage src, int gamma) { if (gamma < 0) { return src; } Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); Mat lookUpTable(1, 256, CV_8U); // 查找表 uchar *p = lookUpTable.ptr(); for (int i = 0; i < 256; ++i) { p[i] = saturate_cast<uchar>(pow(i / 255.0, gamma / 100.0) * 255.0); // pow()是幂次运算 } LUT(srcImg, lookUpTable, dstImg); // LUT QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Log(QImage src, int c) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); Mat lookUpTable(1, 256, CV_8U); // 查找表 uchar *p = lookUpTable.ptr(); for (int i = 0; i < 256; ++i) { p[i] = saturate_cast<uchar>((c / 100.0) * log(1 + i / 255.0) * 255.0); // pow()是幂次运算 } LUT(srcImg, lookUpTable, dstImg); // LUT QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Histeq(QImage src) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); if (srcImg.channels() != 1) { cvtColor(srcImg, srcImg, COLOR_BGR2GRAY); } else { dstImg = srcImg.clone(); } equalizeHist(srcImg, dstImg); // 直方图均衡化 QImage dst = cvMatToQImage2(dstImg); return dst; } void CVPublic::BinToGraylevel(Mat &image) { int w = image.cols; int h = image.rows; for (int row = 0; row < h; row++) { uchar *uc_pixel = image.data + static_cast<unsigned long>(row) * image.step; for (int col = 0; col < w; col++) { if (uc_pixel[0] <= 20) { uc_pixel[0] = 50; } if (uc_pixel[0] >= 235) { uc_pixel[0] = 150; } uc_pixel += 1; } } } bool CVPublic::IsBin(Mat &image) { int w = image.cols; int h = image.rows; for (int row = 0; row < h; row++) { uchar *uc_pixel = image.data + static_cast<unsigned long>(row) * image.step; for (int col = 0; col < w; col++) { if ((uc_pixel[0] >= 20) && (uc_pixel[0] <= 235)) { qDebug() << uc_pixel[0]; return false; } uc_pixel += 3; } } return true; } QImage CVPublic::Erode(QImage src, int elem, int kernel, int times) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); int erosion_type = 0; if (elem == 0) { erosion_type = MORPH_RECT; } else if (elem == 1) { erosion_type = MORPH_CROSS; } else if (elem == 2) { erosion_type = MORPH_ELLIPSE; } Mat element = getStructuringElement( erosion_type, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); erode(srcImg, dstImg, element, Point(-1, -1), times); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Dilate(QImage src, int elem, int kernel, int times) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); int dilation_type = 0; if (elem == 0) { dilation_type = MORPH_RECT; } else if (elem == 1) { dilation_type = MORPH_CROSS; } else if (elem == 2) { dilation_type = MORPH_ELLIPSE; } Mat element = getStructuringElement(dilation_type, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); dilate(srcImg, dstImg, element, Point(-1, -1), times); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Open(QImage src, int elem, int kernel, int times) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); Mat element = getStructuringElement(elem, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); morphologyEx(srcImg, dstImg, MORPH_OPEN, element, Point(-1, -1), times); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Close(QImage src, int elem, int kernel, int times) { Mat srcImg, dstImg; srcImg = QImageTocvMat2(src); Mat element = getStructuringElement(elem, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); morphologyEx(srcImg, dstImg, MORPH_CLOSE, element, Point(-1, -1), times); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Grad(QImage src, int elem, int kernel) { Mat srcImg, grayImg, dstImg; srcImg = QImageTocvMat2(src); Mat element = getStructuringElement(elem, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); if (srcImg.channels() != 1) { cvtColor(srcImg, grayImg, COLOR_BGR2GRAY); } else { grayImg = srcImg.clone(); } morphologyEx(grayImg, dstImg, MORPH_GRADIENT, element); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Tophat(QImage src, int elem, int kernel) { Mat srcImg, grayImg, dstImg; srcImg = QImageTocvMat2(src); Mat element = getStructuringElement(elem, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); if (srcImg.channels() != 1) { cvtColor(srcImg, grayImg, COLOR_BGR2GRAY); } else { grayImg = srcImg.clone(); } morphologyEx(grayImg, dstImg, MORPH_TOPHAT, element); QImage dst = cvMatToQImage2(dstImg); return dst; } QImage CVPublic::Blackhat(QImage src, int elem, int kernel) { Mat srcImg, grayImg, dstImg; srcImg = QImageTocvMat2(src); Mat element = getStructuringElement(elem, Size(2 * kernel + 3, 2 * kernel + 3), Point(kernel + 1, kernel + 1)); if (srcImg.channels() != 1) { cvtColor(srcImg, grayImg, COLOR_BGR2GRAY); } else { grayImg = srcImg.clone(); } morphologyEx(grayImg, dstImg, MORPH_BLACKHAT, element); QImage dst = cvMatToQImage2(dstImg); return dst; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。