11

【opencv 六】利用 opencv 做边缘提取,并展示像素级操作

 3 years ago
source link: https://bbs.cvmart.net/articles/246
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【opencv 六】利用 opencv 做边缘提取,并展示像素级操作

1年前 ⋅ 1438 ⋅ 0 ⋅ 0

利用opencv做一些计算机视觉的操作。实现的功能就是将彩色图片变成灰白的,并对灰度图片作边缘化提取操作。下图展示的是灰度图和边缘图。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

int main()
{
    Mat img_rgb, img_gry, img_cny;

    namedWindow("Example Gray",WINDOW_AUTOSIZE);
    namedWindow("Example Canny",WINDOW_AUTOSIZE);

    img_rgb = imread("H:\\vs2017\\opencv_learning\\ConsoleApplication1\\img.jpg");

    cvtColor(img_rgb,img_gry,COLOR_BGR2GRAY);
    imshow("Example Gray",img_gry);

    Canny(img_gry,img_cny,10,100,3,true);
    imshow("Example Canny",img_cny);

    waitKey(0);

    return 0;

}



对图片的某一个像素进行读取,并修改该像素值。下边代码展示的是对衣服全灰的图,在某一个像素位置使其变成一个小白点。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;

int main()
{
    Mat img_rgb, img_gry, img_cny;

    namedWindow("Example Gray",WINDOW_AUTOSIZE);
    namedWindow("Example Canny",WINDOW_AUTOSIZE);

    img_rgb = imread("H:\\vs2017\\opencv_learning\\ConsoleApplication1\\img.png");

    cvtColor(img_rgb,img_gry,COLOR_BGR2GRAY);
    int x = 16, y = 32;
    Vec3b intensity = img_rgb.at< Vec3b >(y, x);
    // ( Note: We could write img_rgb.at< cv::Vec3b >(x,y)[0] )
    //
    uchar blue = intensity[0];
    uchar green = intensity[1];
    uchar red = intensity[2];

    std::cout << "At (x,y) = (" << x << ", " << y <<
        "): (blue, green, red) = (" <<
        (unsigned int)blue <<
        ", " << (unsigned int)green << ", " <<
        (unsigned int)red << ")" << std::endl;

    std::cout << "Gray pixel there is: " <<
        (unsigned int)img_gry.at<uchar>(y, x) << std::endl;

    img_gry.at<uchar>(x, y) = 255; // Set the gray pixel

    imshow("Example Gray",img_gry);

    Canny(img_gry,img_cny,10,100,3,true);
    imshow("Example Canny",img_cny);

    waitKey(0);

    getchar();
    return 0;

}

版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK