日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Learning OpenCV Lecture 4 (Transforming Images with Morphological Operations)

發布時間:2025/5/22 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Learning OpenCV Lecture 4 (Transforming Images with Morphological Operations) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
In this chapter, we will cover:
  • Eroding and dilating images using morphological filters
  • Opening and closing images using morphological filters
  • Detecting edges and corners using morphological filters
  • Segmenting images using watersheds 分水嶺算法
  • Extracting foreground objects with the GrabCut algorithm
Eroding、dilating、Opening、closing #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp>int main() {// Read input imagecv::Mat image= cv::imread("../binary.bmp" );if (!image.data)return 0;// Display the imagecv::namedWindow( "Image");cv::imshow( "Image",image);// Erode the imagecv::Mat eroded;cv::erode(image,eroded,cv::Mat());// Display the eroded imagecv::namedWindow( "Eroded Image");cv::imshow( "Eroded Image",eroded);// Dilate the imagecv::Mat dilated;cv::dilate(image,dilated,cv::Mat());// Display the dialted imagecv::namedWindow( "Dilated Image");cv::imshow( "Dilated Image",dilated);// Erode the image with a larger s.e.cv::Mat element(7,7,CV_8U,cv::Scalar(1));cv::erode(image,eroded,element);// Display the eroded imagecv::namedWindow( "Eroded Image (7x7)");cv::imshow( "Eroded Image (7x7)",eroded);// Erode the image 3 times.cv::erode(image,eroded,cv::Mat(),cv::Point(-1,-1),3);// Display the eroded imagecv::namedWindow( "Eroded Image (3 times)");cv::imshow( "Eroded Image (3 times)",eroded);// Close the imagecv::Mat element5(5,5,CV_8U,cv::Scalar(1));cv::Mat closed;cv::morphologyEx(image,closed,cv::MORPH_CLOSE,element5);// Display the opened imagecv::namedWindow( "Closed Image");cv::imshow( "Closed Image",closed);// Open the imagecv::Mat opened;cv::morphologyEx(image,opened,cv::MORPH_OPEN,element5);// Display the opened imagecv::namedWindow( "Opened Image");cv::imshow( "Opened Image",opened);// Close and Open the imagecv::morphologyEx(image,image,cv::MORPH_CLOSE,element5);cv::morphologyEx(image,image,cv::MORPH_OPEN,element5);// Display the close/opened imagecv::namedWindow( "Closed and Opened Image");cv::imshow( "Closed and Opened Image",image);cv::imwrite( "binaryGroup.bmp",image);// Read input imageimage= cv::imread("../binary.bmp");// Open and Close the imagecv::morphologyEx(image,image,cv::MORPH_OPEN,element5);cv::morphologyEx(image,image,cv::MORPH_CLOSE,element5);// Display the close/opened imagecv::namedWindow( "Opened and Closed Image");cv::imshow( "Opened and Closed Image",image);cv::waitKey();return 0; }

  results:

?

Detecting edges and corners using morphological filters morphoFeatures.h #if !defined MORPHOF #define MORPHOF#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>class MorphoFeatures { private:// threshold to produce binary imageint threshold;// structuring elements used in corner detectioncv::Mat cross;cv::Mat diamond;cv::Mat square;cv::Mat x;public:MorphoFeatures() : threshold(-1),cross(5, 5, CV_8U, cv::Scalar(0)),diamond(5, 5, CV_8U, cv::Scalar(0)),square(5, 5, CV_8U, cv::Scalar(0)),x(5, 5, CV_8U, cv::Scalar(0)) {// Creating the cross-shaped structuring elementfor (int i = 0; i < 5; i++) {cross.at<uchar>(2, i) = 1;cross.at<uchar>(i, 2) = 1;}// Creating the diamond-shaped structuring elementdiamond.at<uchar>(0, 0) = 0;diamond.at<uchar>(0, 1) = 0;diamond.at<uchar>(1, 0) = 0;diamond.at<uchar>(4, 4) = 0;diamond.at<uchar>(3, 4) = 0;diamond.at<uchar>(4, 3) = 0;diamond.at<uchar>(4, 0) = 0;diamond.at<uchar>(4, 1) = 0;diamond.at<uchar>(3, 0) = 0;diamond.at<uchar>(0, 4) = 0;diamond.at<uchar>(0, 3) = 0;diamond.at<uchar>(1, 4) = 0;// Creating the x-shaped structuring elementfor (int i = 0; i < 5; i++) {x.at<uchar>(i, i) = 1;x.at<uchar>(4 - i, i) = 1;}}void setThreshold(int t) {if (t > 0)threshold = t;}int getThreshold() const {return threshold;}cv::Mat getEdges(const cv::Mat &image) {// Get the gradient imagecv::Mat result;cv::morphologyEx(image, result, cv::MORPH_GRADIENT, cv::Mat());// Apply threshold to obtain a binary imageapplyThreshold(result);return result;}void applyThreshold(cv::Mat &result) {// Apply threshold on resultif (threshold > 0) {cv::threshold(result, result, threshold, 255, cv::THRESH_BINARY_INV);}}cv::Mat getCorners(const cv::Mat &image) {cv::Mat result;// Dilate with a crosscv::dilate(image, result, cross);// Erode with a diamondcv::erode(result, result, diamond);cv::Mat result2;// Dilate with a xcv::dilate(image, result2, x);// Erode with a squarecv::erode(result2, result2, square);// Corners are obtained by differencing// the two closed imagescv::absdiff(result2, result, result);// Apply threshold to obtain a binary imageapplyThreshold(result);return result;}void drawOnImage(const cv::Mat &binary, cv::Mat &image) {cv::Mat_<uchar>::const_iterator it = binary.begin<uchar>();cv::Mat_<uchar>::const_iterator itend = binary.end<uchar>();// for each pixelfor (int i = 0; it != itend; ++it, ++i) {if (!*it) {cv::circle(image,cv::Point(i%image.step, i/image.step),5, cv::Scalar(255, 0, 0));}}}};#endif

  morph.cpp

#include <iostream>#include "morphoFeatures.h"int main() {cv::Mat image = cv::imread( "../building.jpg");cv::cvtColor(image, image, CV_BGR2GRAY);// Create the morphological features instanceMorphoFeatures morpho;morpho.setThreshold(40);// Get the edgescv::Mat edges;edges = morpho.getEdges(image);cv::namedWindow( "Edges Image", CV_WINDOW_AUTOSIZE);cv::imshow( "Edges Image", edges);// Get the cornerscv::Mat corners;corners = morpho.getCorners(image);// Display the corner on the imagemorpho.drawOnImage(corners, image);cv::namedWindow( "Corners on Image", CV_WINDOW_AUTOSIZE);cv::imshow( "Corners on Image", image);cv::waitKey(0);return 0;}

  results:

?

Segmenting images using watersheds watershedSegment.h #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>class WatershedSegmenter { private:cv::Mat markers;public:void setMarkers(const cv::Mat &markerImage) {// Convert to image of intsmarkerImage.convertTo(markers, CV_32S);}cv::Mat process(const cv::Mat &image) {// Apply watershedcv::watershed(image, markers);return markers;}// Return result in the form of an imagecv::Mat getSegmentation() {cv::Mat tmp;// all segment with label higher than 255// will be assigned value 255markers.convertTo(tmp, CV_8U);return tmp;}// Return watershed in the form of an imagecv::Mat getWatersheds() {cv::Mat tmp;// Each pixel p is transform into// 255p + 255 befor conversionmarkers.convertTo(tmp, CV_8U, 255, 255);return tmp;} };

  

// Read input imagecv::Mat image = cv::imread( "../group.jpg");if (!image.data) {return 0;}// Display the imagecv::namedWindow( "Original Image");cv::imshow( "Original Image", image);// Get the binary imagecv::Mat binary;binary = cv::imread( "../binary.bmp", 0);// Display the binary imagecv::namedWindow( "Binary Image");cv::imshow( "Binary Image", binary);// Eliminate noise and smaller objectscv::Mat fg;cv::erode(binary, fg, cv::Mat(), cv::Point(-1, -1), 6);// Display the foreground imagecv::namedWindow( "Foreground Image");cv::imshow( "Foreground Image", fg);

  results:

// Identify image pixels without objectscv::Mat bg;cv::dilate(binary, bg, cv::Mat(), cv::Point(-1, -1), 6);cv::threshold(bg, bg, 1, 128, cv::THRESH_BINARY_INV);// Display the backgroud imagecv::namedWindow( "Background Image");cv::imshow( "Background Image", bg);

  results:

// Show markers imagecv::Mat markers(binary.size(), CV_8U, cv::Scalar(0));markers = fg + bg;cv::namedWindow( "Markers");cv::imshow( "Markers", markers);

  

// Create watershed segmentation objectWatershedSegmenter segmenter;// Set markers and processsegmenter.setMarkers(markers);segmenter.process(image);// Display segmentation resultcv::namedWindow( "Segmentation");cv::imshow( "Segmentation", segmenter.getSegmentation());// Display watershedscv::namedWindow( "Watershed");cv::imshow( "Watershed", segmenter.getWatersheds());

  

// Open another image------------------------------------image = cv::imread( "../tower.jpg");// Identify background pixelscv::Mat imageMask(image.size(), CV_8U, cv::Scalar(0));cv::rectangle(imageMask, cv::Point(5, 5), cv::Point(image.cols - 5, image.rows - 5), cv::Scalar(255), 3);// Identify forground pixels (in the middle of the image)cv::rectangle(imageMask, cv::Point(image.cols / 2 - 10, image.rows / 2 - 10),cv::Point(image.cols / 2 + 10, image.rows / 2 + 10), cv::Scalar(1), 10);// Set markers and processsegmenter.setMarkers(imageMask);segmenter.process(image);// Display the image with markerscv::rectangle(image, cv::Point(5, 5), cv::Point(image.cols - 5, image.rows - 5), cv::Scalar(255, 255, 255), 3);cv::rectangle(image, cv::Point(image.cols / 2 - 10, image.rows / 2 - 10),cv::Point(image.cols / 2 + 10, image.rows / 2 + 10), cv::Scalar(1, 1, 1), 10);cv::namedWindow( "Image with marker");cv::imshow( "Image with marker", image);// Display watershedscv::namedWindow( "Watersheds of foreground object");cv::imshow( "Watersheds of foreground object", segmenter.getWatersheds());

  results:

?

Extracting foreground objects with the GrabCut algorithm // Open another imageimage = cv::imread( "../tower.jpg");// define bounding rectangecv::Rect rectangle(50, 70, image.cols - 150, image.rows - 180);cv::Mat result; // segmentation result (4 possible values)cv::Mat bgModel, fgModel; // the models (internally used)// GrabCut segmentationcv::grabCut(image, // input imageresult, // segmentation resultrectangle, // rectangle containing foregroundbgModel, fgModel, // models1, //number of iterationscv::GC_INIT_WITH_RECT// use rectangle);// Get the pixles marked as likely foregroundcv::compare(result, cv::GC_PR_FGD, result, cv::CMP_EQ);// Generate output imagecv::Mat foreground(image.size(), CV_8UC3, cv::Scalar(255, 255, 255));image.copyTo(foreground, result); // bg pixels not copied// draw rectangle on original imagecv::rectangle(image, rectangle, cv::Scalar(255,255,255),1);cv::namedWindow( "Image");cv::imshow( "Image",image);// display resultcv::namedWindow( "Segmented Image");cv::imshow( "Segmented Image",foreground);

  

// Open another imageimage= cv::imread("../group.jpg");// define bounding rectanglecv::Rect rectangle2(10,100,380,180);cv::Mat bkgModel,fgrModel; // the models (internally used)// GrabCut segmentationcv::grabCut(image, // input imageresult, // segmentation resultrectangle2,bkgModel,fgrModel,5,cv::GC_INIT_WITH_RECT);// Get the pixels marked as likely foreground// cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);result= result&1;foreground.create(image.size(),CV_8UC3);foreground.setTo(cv::Scalar(255,255,255));image.copyTo(foreground,result); // bg pixels not copied// draw rectangle on original imagecv::rectangle(image, rectangle2, cv::Scalar(255,255,255),1);cv::namedWindow( "Image 2");cv::imshow( "Image 2",image);// display resultcv::namedWindow( "Foreground objects");cv::imshow( "Foreground objects",foreground);

  

轉載于:https://www.cnblogs.com/starlitnext/p/3861398.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Learning OpenCV Lecture 4 (Transforming Images with Morphological Operations)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 韩国av中文字幕 | 日韩欧美精品一区二区三区 | 欧美一区二区三区视频在线观看 | 中文字幕日本一区二区 | 成人亚洲黄色 | 最好看的2019中文大全在线观看 | 日韩欧美不卡在线 | 国产免费一区二区三区三州老师 | 伊人婷婷久久 | 国产亚洲成人av | 亚洲精品乱码久久久久久写真 | 精品国产乱码久久久久久蜜臀 | 秋霞国产精品 | 美国毛片av | 国产欧美一区二区三区精品酒店 | 日本黄色片 | 欧美特级黄色大片 | 婷婷视频一区 | 特级av片 | 特级大胆西西4444人体 | 男女啪动最猛动态图 | 国产亚洲精品久久久 | 18p在线观看 | 人妻在线日韩免费视频 | 加勒比hezyo黑人专区 | 性开放的欧美大片 | 午夜在线不卡 | 朋友的姐姐2在线观看 | 亚洲制服丝袜一区 | 欧美国产激情 | 欧洲久久久久 | av片在线免费看 | 日韩经典中文字幕 | 久操欧美 | 落日余晖图片 | 中文字幕五区 | 国产福利免费看 | 一区二区三区视频免费在线观看 | 国产精品电影网站 | 综合激情视频 | 美女被娇喘流出白 | 伦理一级片 | 三级网站免费看 | 在线麻豆av | 国产午夜一级一片免费播放 | 色视频一区二区 | 国产网站一区 | 免费在线观看成人 | 日本高清不卡视频 | 日本公与丰满熄 | 中文字幕亚洲第一 | 久一视频在线 | 在线看的网站 | 久久久久久蜜桃一区二区 | 69午夜 | 色屁屁一区二区三区 | 大桥未久av一区二区三区中文 | 日韩大片一区 | 色诱视频在线观看 | 99一级片| 九色.com | 九九热视频免费观看 | www.色就是色 | 少妇免费视频 | www狠狠爱 | 亚洲自拍偷拍区 | 成人午夜久久 | 亚洲中文一区二区三区 | 国产视频在线观看网站 | 亚洲午夜久久 | 91伊人久久 | 三级黄在线观看 | 谁有毛片网址 | 宅男午夜在线 | 国语对白对话在线观看 | 国产精品sm调教免费专区 | 日韩精品人妻一区 | а√天堂8资源在线官网 | 日本高清不卡二区 | www.欧美com| 一级成人av | www.中文字幕在线观看 | 人妻在线一区 | 九九久久九九久久 | 国产精品视频久久久 | 亚洲日本成人在线观看 | 欧美另类第一页 | 婷婷久久综合网 | 成人h视频| 福利电影一区二区 | 久久精品一区二区国产 | 色婷婷综合久久久久中文字幕 | 欧美aaaaa| 高清视频在线免费观看 | 妖精视频污 | 91午夜精品 | 性av网站 | 91美女诱惑 | 久久夜色精品国产欧美乱 |