将红色通道与归一化图像结合

时间:2018-09-12 14:29:56

标签: c++ opencv image-processing

使用霍夫变换后,我得到了这个结果(image 1

https://coderwall.com/p/rvduyw/jdbc-inserting-unicode-utf-8-characters-into-mysql

现在我要在归一化图像中获得此检测到的区域(在绿色通道中检测到的区域)。

这正是我想要做的(image 2image

我使用bitwise_and,但是它不起作用

代码在这里:

/// Global variables
Mat dst, outimg, nrmimg;

int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;
Mat red, red1, blue, green;

char* window_name = "Morphology Transformations Demo";

int main() {
    //read image
    cv::Mat img = imread("C:\\Users\\ASUS\\Desktop\\eye.jpg ", CV_LOAD_IMAGE_COLOR);

    cv::imshow("thinng10", img);
    // resize
    cv::resize(img, outimg, cv::Size(720, 576));

    cv::imshow("resize", outimg);
    // normalisation
    cv::normalize(outimg, nrmimg, 0, 255, NORM_MINMAX, -1);

    cv::imshow("normz", nrmimg);
    // SPlit
    Mat bgr[3];   //destination array
    split(nrmimg, bgr);//split source  

    blue.push_back(bgr[0]);
    green.push_back(bgr[1]);
    red.push_back(bgr[2]);
    cv::imshow("green", green);
    cv::imshow("red", red);

    // pass-low filters
    Mat plimg;
    float sum;

    // define the kernel
    float Kernel[9][9] = {
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 },
        { 1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0,1 / 9.0, 1 / 9.0, 1 / 9.0 }
    };
    plimg = green.clone();
    for (int y = 0; y < green.rows; y++)
        for (int x = 0; x < green.cols; x++)
            plimg.at<uchar>(y, x) = 0.0;
    //convolution operation
    for (int y = 1; y < green.rows - 1; y++) {
        for (int x = 1; x < green.cols - 1; x++) {
            sum = 0.0;
            for (int k = -1; k <= 1;k++) {
                for (int j = -1; j <= 1; j++) {
                    sum = sum + Kernel[j + 1][k + 1] * green.at<uchar>(y - j, x - k);
                }
            }
            plimg.at<uchar>(y, x) = sum;
        }
    }

    namedWindow("pass-low-image");
    imshow("pass-low-image", plimg);

    /// transformation de fourier 
    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize(plimg.rows);
    int n = getOptimalDFTSize(plimg.cols); // on the border add zero values
    copyMakeBorder(green, padded, 0, m - plimg.rows, 0, n - plimg.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
    Mat complexI;
    merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

    dft(complexI, complexI);            // this way the result may fit in the source matrix

                                        // compute the magnitude and switch to logarithmic scale
                                        // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
    split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
    Mat magI = planes[0];

    magI += Scalar::all(1);                    // switch to logarithmic scale
    log(magI, magI);

    // crop the spectrum, if it has an odd number of rows or columns
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = magI.cols / 2;
    int cy = magI.rows / 2;

    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

    Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
    q2.copyTo(q1);
    tmp.copyTo(q2);

    normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
                                            // viewable image form (float between values 0 and 1).
                                            // Show the result
    imshow("spectrum magnitude", magI);
    Mat inverseTransform1;

    dft(magI, inverseTransform1, DFT_INVERSE | DFT_REAL_OUTPUT);

    cv::Mat out1, out2;
    inverseTransform1.convertTo(out1, CV_8U);

    namedWindow("result image 1");
    imshow("result image 1", out1);

    double thresh = 50;
    double maxValue = 255;

    // ROI AUTRE METHODE 
    int px2 = 500;
    int py2 = 250;
    Point p1 = cv::Point(px2, py2);
    Mat maskcirc = cv::Mat::zeros(red.size(), red.type());
    cv::circle(maskcirc, p1, 200, 50, -1);
    Mat image2(red.size(), CV_32F);
    red.copyTo(image2, maskcirc);
    imshow("img", image2);

    //closing
    Mat element = getStructuringElement(MORPH_ELLIPSE, Size(6, 6));
    Mat closeImg;

    morphologyEx(image2, closeImg, MORPH_CLOSE, element);
    namedWindow("CLOSE");
    imshow("CLOSE", closeImg);

    Mat openImg;

    morphologyEx(closeImg, openImg, MORPH_OPEN, element);
    namedWindow("open");
    imshow("open", openImg);
    //filtre prewitt

    //GaussianBlur(openImg, openImg, Size(9,9), 0, 0, BORDER_DEFAULT);
    //GaussianBlur(openImg, openImg, Size(9, 9), 0, 0);
    vector<Vec3f> circles;

    /// Apply the Hough Transform to find the circles;
    HoughCircles(openImg, circles, CV_HOUGH_GRADIENT, 1,1,67, 17,35, 80);

    /// Draw the circles detected
    /*for (size_t i = 0; i < circles.size(); i++)
    {
        Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
        int radius = cvRound(circles[i][2]);
        // circle center
        circle(openImg, center, 1, Scalar(255, 255, 255), -1, 8, 0);
        // circle outline
        circle(openImg, center, radius, Scalar(255, 255, 255), 1, 4, 0);

    }*/
    Rect bounding_rect;
    // remember biggest radius
    float radiusBiggest = 0;
    // remember the index of the biggest radius / circle
    int indexBiggest = -1;
    // loop through all circles
    for (size_t i = 0; i < circles.size(); i++)
    {
        // get the radius
        float radius = circles[i][2];
        // check if this radius is bigger than any previous
        if (radius > radiusBiggest)
        {
            // this radius/circle is the biggest so far. remember it
            radiusBiggest = radius;
            indexBiggest = i;
        }
    }
    // if we found a circle then draw it
    if (indexBiggest != -1)
    {
        Point center(cvRound(circles[indexBiggest][0]), cvRound(circles[indexBiggest][1]));
        int radius = cvRound(circles[indexBiggest][2]);
        // circle center
        circle(openImg, center, 1, Scalar(255, 255, 255), -1, 8, 0);
        // circle outline
        circle(openImg, center,radius, Scalar(255, 255, 255), 1, 4, 0);

        cv::Mat mask = cv::Mat::zeros(openImg.rows,openImg.cols, CV_8UC1);
        circle(mask, center,radius, Scalar(255, 255, 255), -1, 8, 0); //-1 means filled
        openImg.copyTo(dst, mask);
    }

    /// Show your results
    namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
    imshow("Hough Circle Transform Demo", dst);

    Mat C;
    bitwise_and(nrmimg, dst, C);

    namedWindow("AND", CV_WINDOW_AUTOSIZE);
    imshow("AND", C);

    cv::waitKey(0);

    return 0;
}

我使用bitwise_and,但是它不起作用

0 个答案:

没有答案