qt下两种方式读取opencv 图片各个通道的值

发布于:2024-09-17 ⋅ 阅读:(146) ⋅ 点赞:(0)

qt下两种方式读取opencv 图片各个通道的值

   Mat srcImg = imread("D:\\1.jpg");
    if(srcImg.empty())
    {
        QMessageBox::information(this,"警告","图片读取失败,请检查图片路径!");
        return;
    }
    Mat imgShow ;
    cvtColor(srcImg, imgShow, COLOR_BGR2RGB); // 图像格式转换
    QImage qImg = QImage((unsigned char*)(imgShow.data), imgShow.cols,
                         imgShow.rows, imgShow.cols*imgShow.channels(), QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(qImg.scaled(ui->label->size(), Qt::KeepAspectRatio)));

    qDebug()<<"通道"<<imgShow.channels()<<endl;
    for(int i=0;i<imgShow.rows;++i)
    {
        uchar*ptr = imgShow.ptr<uchar>(i);

        for(int j =0;j<imgShow.cols;++j)
        {
            qDebug()<<static_cast<int>(ptr[j*3+0])<< static_cast<int>(ptr[j*3+1])<<static_cast<int>(ptr[j*3+2])<<endl;
            //ptr[j*3+0] = 255;
            //可以在此直接修改像素的值
      
        }
    }

    for(int i = 0; i < imgShow.rows; ++i) {
        for(int j = 0; j < imgShow.cols; ++j) {
            // 获取(i, j)位置的像素值
            cv::Vec3b color = imgShow.at<cv::Vec3b>(i, j);
            // 打印各个通道的值
            std::cout << "Pixel at (" << i << ", " << j << "): "
                      << "B = " << static_cast<int>(color[0]) << ", "
                      << "G = " << static_cast<int>(color[1]) << ", "
                      << "R = " << static_cast<int>(color[2]) << std::endl;
        }
    }

网站公告

今日签到

点亮在社区的每一天
去签到