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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

OpenCV(一)图像读取与新建、图像显示、操作图像像素(2种涂色并比较算法优劣、输出RGB)

發(fā)布時(shí)間:2023/11/27 生活经验 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV(一)图像读取与新建、图像显示、操作图像像素(2种涂色并比较算法优劣、输出RGB) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

一、讀取圖像與新建圖像

1、讀取圖像

2、新建圖像

二、顯示圖像

1、過(guò)程

2、代碼

3、運(yùn)行效果

三、操作圖像像素

1、逐RGB涂色(單循環(huán))(快)

1-1、過(guò)程

2-2、代碼?

2-3、運(yùn)行結(jié)果

2、逐行涂色(雙循環(huán))(慢)

3、算法快慢比較

總代碼


一、讀取圖像與新建圖像

注:如果是添加新圖(非讀取),必須要新建圖像,否則會(huì)報(bào)錯(cuò)(沒(méi)有初值)。?

總而言之,圖像必須有初值,這個(gè)初值要么讀取獲得,要么新建獲得。

1、讀取圖像

img = imread("Resource/test.jpg");

2、新建圖像

dst = Mat::zeros(img.size(), img.type());

分別需要填入圖片大小圖片類(lèi)型 。

二、顯示圖像

1、過(guò)程

1、打開(kāi)圖像;

2、新建窗口

3、在窗口中顯示圖像

2、代碼

//顯示圖像
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;int main()
{//1、打開(kāi)圖像Mat img = imread("Resource/test.png");//圖像為空if (img.empty())cout << "can not find the image!\n";//設(shè)置窗口(可以不要)namedWindow("圖像", WINDOW_AUTOSIZE);		//新建一個(gè)顯示窗口,命名并指定窗口的類(lèi)型(這里為固定)//											WINDOW_NORMAL		//可調(diào)控//2、在窗口中顯示imshow("圖像", img);waitKey(0);				//等待
}

3、運(yùn)行效果

三、操作圖像像素

三通道RGB):

1、逐RGB涂色(單循環(huán))(快)

1-1、過(guò)程

指針指向首元素地址

//獲取矩陣數(shù)據(jù)的起始地址
uchar* p = image.ptr<uchar>(0);
//圖像的指針用法舉例
cv::Mat image = cv::Mat(400, 600, CV_8UC1);    //定義了一個(gè)Mat變量image。
uchar * data00 = image.ptr<uchar>(0);          //data00是指向image第1行第1個(gè)元素的指針。
uchar * data10 = image.ptr<uchar>(1);          //data10是指向image第1行第1個(gè)元素的指針。
uchar * data01 = image.ptr<uchar>(0)[1];       //data01是指向image第1行第2個(gè)元素的指針。

一共運(yùn)行length次,lengthrgb總數(shù)

//總RGB個(gè)數(shù)int length = image.cols * image.rows * image.channels();//總RGB個(gè)數(shù) = 行數(shù)*列數(shù)*通道數(shù)	  (總像素=行數(shù)*列數(shù)	轉(zhuǎn)化為RGB->乘RGB數(shù)量(即通道數(shù))//三通道:RGB

2-2、代碼?

//逐RGB涂色(單循環(huán))
void setAllWhite(Mat& image)
{int i;//總RGB個(gè)數(shù)int length = image.cols * image.rows * image.channels();//總RGB個(gè)數(shù) = 行數(shù) * 列數(shù) * 通道數(shù)					(總像素=行數(shù)*列數(shù)		轉(zhuǎn)化為RGB->乘RGB數(shù)量(即通道數(shù))//三通道:RGB//獲取矩陣數(shù)據(jù)的起始地址uchar* p = image.ptr<uchar>(0);///逐rgb涂色for (i = 0; i < length; i++){//data[i] = 150;				(*p++) = 150;				//涂色(逐RGB)}
}

2-3、運(yùn)行結(jié)果

2、逐行涂色(雙循環(huán))(慢)

(同上)指針指向首元素地址

//獲取矩陣數(shù)據(jù)的起始地址
uchar* p = image.ptr<uchar>(0);
//圖像的指針用法舉例
cv::Mat image = cv::Mat(400, 600, CV_8UC1);    //定義了一個(gè)Mat變量image。
uchar * data00 = image.ptr<uchar>(0);          //data00是指向image第1行第1個(gè)元素的指針。
uchar * data10 = image.ptr<uchar>(1);          //data10是指向image第1行第1個(gè)元素的指針。
uchar * data01 = image.ptr<uchar>(0)[1];       //data01是指向image第1行第2個(gè)元素的指針。

?逐行 涂色image.rows,再在循環(huán)中逐rgb涂色image.cols * image.channels()。

//逐行涂色(雙循環(huán))
void setAllWhiteRows(Mat& image)
{int i, j;//獲取矩陣數(shù)據(jù)的起始地址uchar* p = image.ptr<uchar>(0);for (i = 0; i < image.rows; i++)										//逐行{for (j = 0; j < image.cols * image.channels(); j++)		//逐rgb{(*p++) = 150;				//涂色(逐RGB)}}
}

3、算法快慢比較

運(yùn)用時(shí)間獲取函數(shù)colck()開(kāi)始時(shí)計(jì)時(shí)一次結(jié)束時(shí)計(jì)時(shí)一次差值即為算法運(yùn)行時(shí)間。

clock()返回ms,需要獲取s,所以除1000

//比較兩種算法的運(yùn)行速度
void compareTime(Mat& image)
{int count = 10;long begin, end;//統(tǒng)計(jì)單循環(huán)方式(逐像素)運(yùn)行時(shí)間begin = clock();while (count-- > 0)setAllWhite(image);				//運(yùn)行end = clock();printf("Single loop time is %f .\n", (double)(end - begin) / (double)CLOCKS_PER_SEC);//																	clock()以ms計(jì),需要除1000才能獲取到s//統(tǒng)計(jì)雙循環(huán)方式(逐行)運(yùn)行時(shí)間count = 10;begin = clock();while (count-- > 0)setAllWhiteRows(image);		//運(yùn)行end = clock();printf("Double loop time is %f \n", (double)(end - begin) / (double)CLOCKS_PER_SEC);
}

總代碼

//圖像逐像素的操作
//單循環(huán):逐個(gè)對(duì)每個(gè)像素進(jìn)行賦值
//雙循環(huán):逐行對(duì)每個(gè)像素進(jìn)行賦值
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;//逐RGB涂色(單循環(huán))
void setAllWhite(Mat& image)
{int i;//總RGB個(gè)數(shù)int length = image.cols * image.rows * image.channels();//總RGB個(gè)數(shù) = 行數(shù) * 列數(shù) * 通道數(shù)					(總像素=行數(shù)*列數(shù)		轉(zhuǎn)化為RGB->乘RGB數(shù)量(即通道數(shù))//三通道:RGB//獲取矩陣數(shù)據(jù)的起始地址uchar* p = image.ptr<uchar>(0);///逐rgb涂色for (i = 0; i < length; i++){//data[i] = 150;				(*p++) = 150;				//涂色(逐RGB)}
}//逐行涂色(雙循環(huán))
void setAllWhiteRows(Mat& image)
{int i, j;//獲取矩陣數(shù)據(jù)的起始地址uchar* p = image.ptr<uchar>(0);for (i = 0; i < image.rows; i++)										//逐行{for (j = 0; j < image.cols * image.channels(); j++)		//逐rgb{(*p++) = 150;				//涂色(逐RGB)}}
}//比較兩種算法的運(yùn)行速度
void compareTime(Mat& image)
{int count = 10;long begin, end;//統(tǒng)計(jì)單循環(huán)方式(逐像素)運(yùn)行時(shí)間begin = clock();while (count-- > 0)setAllWhite(image);				//運(yùn)行end = clock();printf("Single loop time is %f .\n", (double)(end - begin) / (double)CLOCKS_PER_SEC);//																	clock()以ms計(jì),需要除1000才能獲取到s//統(tǒng)計(jì)雙循環(huán)方式(逐行)運(yùn)行時(shí)間count = 10;begin = clock();while (count-- > 0)setAllWhiteRows(image);		//運(yùn)行end = clock();printf("Double loop time is %f \n", (double)(end - begin) / (double)CLOCKS_PER_SEC);
}//輸出RGB
void Print(Mat& image)
{int i, length;const uchar* p = image.ptr<uchar>(0);		//指向首個(gè)指針length = image.rows;									//*image.cols		這里只輸出一行cout << "\nPrint one row pixels:\n";for (i = 0; i < length; i++){printf("%2d", *p++);}cout << endl;
}int main()
{Mat img = imread("Resource/test.png");			//打開(kāi)圖片if (img.empty()){printf("could not load the image..");return -1;}//顯示圖像namedWindow("初始圖像", WINDOW_AUTOSIZE);imshow("初始圖像", img);Print(img);									//輸出RGB//涂色與算法比較compareTime(img);						//比較兩種算法運(yùn)行時(shí)間//setAllWhite(img);							//逐RGB涂色//setAllWhiteRows(img);					//逐行涂色//顯示圖像namedWindow("涂色圖像", WINDOW_AUTOSIZE);imshow("涂色圖像", img);Print(img);										//輸出RGBwaitKey(0);										//等待
}

?

總結(jié)

以上是生活随笔為你收集整理的OpenCV(一)图像读取与新建、图像显示、操作图像像素(2种涂色并比较算法优劣、输出RGB)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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