opencv中HSI读取视频不能成功,代码运行成功,结果出不来。

#include
//#include
#include
#include

using namespace std;
using namespace cv;

//-------------------------- RGB 模型 转换为 HSI 模型--------------------//
int main(){
VideoCapture capture(0);
printf("摄像头调用成功");
if(!capture.isOpened())

{

return -1;

}  
 //Mat special;
  while(1){
    Mat BGRImage,HSIImage,temp;              //存储每一帧的图像
    capture >>BGRImage ;       //读取当前帧
     if (!BGRImage.empty()) //判断当前帧是否捕捉成功 **这步很重要  
        imshow("name", BGRImage); //若当前帧捕捉成功,显示  
    else  
        cout<<"can not ";   
    //temp = BGRImage.clone();
    Mat fhsi;
//Mat BGRImage = imread("2.jpg");
HSIImage = Mat(Size(BGRImage.cols, BGRImage.rows), CV_8UC3);
imshow("video",BGRImage);
//namedWindow(",LeafBGRImage",CV_WINDOW_AUTOSIZE);
vector <Mat> channels;
split(HSIImage, channels);
Mat Hvalue = channels.at(0);
Mat Svalue = channels.at(1);
Mat Ivalue = channels.at(2);

for (int i = 0; i < BGRImage.rows; ++i)
    for (int j = 0; j < BGRImage.cols; ++j)
    {
        double H, S, I;
        int Bvalue = BGRImage.at<Vec3b>(i, j)(0);
        int Gvalue = BGRImage.at<Vec3b>(i, j)(1);
        int Rvalue = BGRImage.at<Vec3b>(i, j)(2);

        //求Theta =acos((((Rvalue - Gvalue) + (Rvalue - Bvalue)) / 2) / sqrt(pow((Rvalue - Gvalue), 2) + (Rvalue - Bvalue)*(Gvalue - Bvalue)));
        double numerator = ((Rvalue - Gvalue) + (Rvalue - Bvalue)) / 2;
        double denominator = sqrt(pow((double)(Rvalue - Gvalue), 2) + (Rvalue - Bvalue)*(Gvalue - Bvalue));
        if (denominator == 0)
            H = 0;
        else{
            double Theta = acos(numerator / denominator) * 180 / 3.14;
            if (Bvalue <= Gvalue)
                 H = Theta;
            else  H = 360 - Theta;
        }       
        Hvalue.at<uchar>(i, j) = (int)(H * 255 / 360); //为了显示将[0~360]映射到[0~255]

        //求S = 1-3*min(Bvalue,Gvalue,Rvalue)/(Rvalue+Gvalue+Bvalue);
        int minvalue = Bvalue;
        if (minvalue > Gvalue) minvalue = Gvalue;
        if (minvalue > Rvalue) minvalue = Rvalue;
        numerator = 3 * minvalue;
        denominator = Rvalue + Gvalue + Bvalue;
        if (denominator == 0)  S = 0;
        else{
            S = 1 - numerator / denominator;
        }
        Svalue.at<uchar>(i, j) = (int)(S * 255);//为了显示将[0~1]映射到[0~255]

        I= (Rvalue + Gvalue + Bvalue) / 3;          
        Ivalue.at<uchar>(i, j) = (int)(I);
    }

merge(channels, HSIImage);
namedWindow("HSI",CV_WINDOW_AUTOSIZE);
imshow("HSI", HSIImage);
  }
waitKey(0);
return 0;

}
图片说明

1个回答

看看是否安装了解码器,若已经安装了,看看是不是图像过大,

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐