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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OPENCV学习笔记2-5_扫描图像并访问相邻像素

發(fā)布時間:2024/9/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OPENCV学习笔记2-5_扫描图像并访问相邻像素 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  To illustrate this recipe, we will apply a processing function that sharpens an image(銳化圖像的處理函數(shù)). This time, the processing cannot be accomplished in-place. Users need to provide an output image. The image scanning(掃描)is done using three pointers, one for the current line, one for the line above, and another one for the line below.

  Since filtering is a common operation in image processing, OpenCV has defined a special function that performs this task: the cv::filter2D function. Note that it is particularly advantageous(特別有利) to use the filter2D function with a large kernel, in this case, a more efficient algorithm.

#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; void sharpen(const Mat &image, Mat &result) ; void sharpen2D(const Mat &image, Mat &result);int main() {Mat image = imread("test.jpg");resize(image, image, Size(), 0.6, 0.6);Mat result1;Mat result2;namedWindow("YunFung Image");imshow("YunFung Image", image);sharpen(image, result1);namedWindow("Result1");imshow("Result1", result1);sharpen2D(image, result2);namedWindow("Result2");imshow("Result2", result2);waitKey();return 0; }void sharpen(const Mat &image, Mat &result) {result.create(image.size(), image.type()); // allocate if necessaryint nchannels = image.channels();for (int j = 1; j < image.rows - 1; j++) { // for all rows (except first and last)const uchar* previous = image.ptr<const uchar>(j - 1); // previous rowconst uchar* current = image.ptr<const uchar>(j); // current rowconst uchar* next = image.ptr<const uchar>(j + 1); // next row uchar* output = result.ptr<uchar>(j); // output row pointer//saturate_cast<uchar> changing negative values to 0 and values over 255 to 255//sharpened_pixel = 5 * current - left - right - up - down;for (int i = nchannels; i < (image.cols - 1)*nchannels; i++) {*output++ = saturate_cast<uchar>(5 * current[i] - current[i - nchannels]- current[i + nchannels] - previous[i] - next[i]);}}// Set the unprocessed pixels to 0 - left - right - up - downresult.row(0).setTo(Scalar(0));result.row(result.rows - 1).setTo(Scalar(0));result.col(0).setTo(Scalar(0));result.col(result.cols - 1).setTo(Scalar(0)); } void sharpen2D(const Mat &image, Mat &result) {// Construct kernel (all entries initialized to 0)Mat kernel(3, 3, CV_32F, Scalar(0));// assigns kernel valueskernel.at<float>(1, 1) = 5.0;kernel.at<float>(0, 1) = -1.0;kernel.at<float>(2, 1) = -1.0;kernel.at<float>(1, 0) = -1.0;kernel.at<float>(1, 2) = -1.0;//filter the image filter2D(image, result, image.depth(), kernel); }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/yunfung/p/7560811.html

總結(jié)

以上是生活随笔為你收集整理的OPENCV学习笔记2-5_扫描图像并访问相邻像素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。