(OpenCV牛逼)OpenCV提供了多种遍历图像像素的方式。
OpenCV版本为4.5.1;测试图像分辨率为1.28亿=16000*8000。
分享给有需要的人,代码质量勿喷。
void OPENCV451::on_actionTraversePixel_triggered()
{
//在OpenCV C++中Mat对象的内存管理由OpenCV框架自动负责内存分配与回收,基于智能指针实现内存管理[1]。
bool mat1 = true, mat2 = true, mat3 = true, mat4 = true;
double time1 = 0, time2 = 0, time3 = 0, time4 = 0;
QString imagePath = QCoreApplication::applicationDirPath() + "/cs1.2.jpg";
cv::Mat img = cv::imread(imagePath.toLocal8Bit().toStdString());
int w = img.cols;
int h = img.rows;
一、比较容易理解,根据行列索引号获取像素值。
//一、基于Mat对象的随机像素访问API实现,通过行列索引方式遍历每个像素值[1]。
if (mat1)
{
cv::Mat img = cv::imread(imagePath.toLocal8Bit().toStdString());
double time1s = (double)cv::getTickCount();
for (int row = 0; row < h; row++)
{
for (int col = 0; col < w; col++)
{
cv::Vec3b bgr = img.at<cv::Vec3b>(row, col);
//Test1:仅遍历像素
int blue = bgr[0];
int green = bgr[1];
int red = bgr[2];
//Test2:改变像素值
bgr[0] = 255 - bgr[0];
bgr[1] = 255 - bgr[1];
bgr[2] = 255 - bgr[2];
img.at<cv::Vec3b>(row, col) = bgr;
}
}
//保存新图
cv::imwrite("F:/mat1.jpg", img, {1, 95});//100质量最高,默认为95
cv::imwrite("F:/mat1.png", img, {16, 1});//0质量最高,默认为1
double time1e = (double)cv::getTickCount();
time1 = (time1e - time1s) / cv::getTickFrequency();
}
二、指针
//二、基于Mat对象的行随机访问指针方式实现对每个像素的遍历[1]。
if (mat2)
{
cv::Mat img = cv::imread(imagePath.toLocal8Bit().toStdString());
double time2s = (double)cv::getTickCount();
for (int row = 0; row < h; row++)
{
cv::Vec3b* vRow = img.ptr<cv::Vec3b>(row);
for (int col = 0; col < w; col++)
{
cv::Vec3b bgr = vRow[col];
//Test1:仅遍历像素
int blue = bgr[0];
int green = bgr[1];
int red = bgr[2];
//Test2:改变像素值
bgr[0] = 255 - bgr[0];
bgr[1] = 255 - bgr[1];
bgr[2] = 255 - bgr[2];
img.at<cv::Vec3b>(row, col) = bgr;
}
}
//保存新图
cv::imwrite("F:/mat2.jpg", img, {1, 95});//100质量最高,默认为95
cv::imwrite("F:/mat2.png", img, {16, 1});//0质量最高,默认为1
double time2e = (double)cv::getTickCount();
time2 = (time2e - time2s) / cv::getTickFrequency();
}
三、效率最高
/* 三、直接获取Mat对象的像素块的数据指针,基于指针操作,实现快速遍历[1]。 */
if (mat3)
{
cv::Mat img = cv::imread(imagePath.toLocal8Bit().toStdString());
double time3s = (double)cv::getTickCount();
for (int row = 0; row < h; row++)
{
uchar* ucBGR = img.data + row * img.step;
for (int col = 0; col < w; col++)
{
//Test1:仅遍历像素
int blue = ucBGR[0];
int green = ucBGR[1];
int red = ucBGR[2];
//Test2:改变像素值
ucBGR[0] = 255 - ucBGR[0];
ucBGR[1] = 255 - ucBGR[1];
ucBGR[2] = 255 - ucBGR[2];
ucBGR += 3;
}
}
//保存新图
cv::imwrite("F:/mat3.jpg", img, {1, 95});//100质量最高,默认为95
cv::imwrite("F:/mat3.png", img, {16, 1});//0质量最高,默认为1
double time3e = (double)cv::getTickCount();
time3 = (time3e - time3s) / cv::getTickFrequency();
}
四、效率最低
//四、迭代器
if (mat4)
{
cv::Mat img = cv::imread(imagePath.toLocal8Bit().toStdString());
double time4s = (double)cv::getTickCount();
cv::Mat_<cv::Vec3b>::iterator it = img.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend = img.end<cv::Vec3b>();
for (; it != itend; ++it)
{
//Test1:仅遍历像素
int blue = (*it)[0];
int green = (*it)[1];
int red = (*it)[2];
//Test2:改变像素值
(*it)[0] = 255 - (*it)[0];
(*it)[1] = 255 - (*it)[1];
(*it)[2] = 255 - (*it)[2];
}
//保存新图
cv::imwrite("F:/mat4.jpg", img, {1, 95});//100质量最高,默认为95
cv::imwrite("F:/mat4.png", img, {16, 1});//0质量最高,默认为1
double time4e = (double)cv::getTickCount();
time4 = (time4e - time4s) / cv::getTickFrequency();
}
QMessageBox::information(0, "time_Test1",
QString::number(time1, 'f', 10) + "\n" +
QString::number(time2, 'f', 10) + "\n" +
QString::number(time3, 'f', 10) + "\n" +
QString::number(time4, 'f', 10));
}
五、对比分析
遍历方式 | Test1:仅遍历像素值 | Test2:修改像素值 | Test3:保存新图 |
mat1 | 0 | 0.6114 | 2.3555 |
mat2 | 0 | 0.6115 | 2.3551 |
mat3 | 0 | 0.1077 | 1.8497 |
mat4 | 0.0676 | 0.2829 | 2.0273 |