当前位置:   article > 正文

OpenCV常用方法_opencvpublic

opencvpublic

OpenCV常用方法

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/103601
推荐阅读
相关标签
  

闽ICP备14008679号