Ask Your Question

Revision history [back]

How to convert my code of IplImage to cv::Mat or How to make my code by using cv::Mat?

I was trying to convert Iplimage structure to cv::Mat class. But I got something wrong error in new code that I fixed.

So, I just want to compare what you guys will fix it with mine. Can you convert my code by using cv::Mat class?

Here is my code that has only IplImage

enter code here
#include<opencv2/core/core_c.h>
#include<opencv2/highgui/highgui_c.h>
#include<opencv/cv.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include<Windows.h>
#include<ctffunc.h> 
void RGB_TO_HSV(unsigned char r, unsigned char g, unsigned char b, double &h, double &s, double &v );
void HSV_TO_RGB(unsigned char h, unsigned char s, unsigned char v, double &r, double &g, double &b);
void FILTER(int H_max,int H_min,int V_max,int V_min,int S_max,int S_min);
void on_change1(int pos1);
void on_change2(int pos2);
void on_change3(int poS3);
void on_change4(int poS4);
void on_change5(int poS5);
void on_change6(int poS6);
IplImage *srcImage,*dstImage,*srcR,*srcG,*srcB,*dstR,*dstG,*dstB;
int H_max,H_min,V_max,V_min,S_max,S_min;
int main()
{   double r, g, b, h, s, v;
    int x,y;
    int pos1=0;
    int pos2=0;
    int pos3=0;
    int pos4=360;
    int pos5=255;
    int pos6=255;   
    if((srcImage=cvLoadImage("sunrise.jpg",1))==NULL)
        return -1;
    srcR=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    srcG=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    srcB=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstR=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstG=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstB=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1); 
    cvSplit(srcImage,srcB,srcG,srcR,NULL);  
    dstImage=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,3);
    cvSetZero(dstImage);
    cvMerge(srcB,srcG,srcR,NULL,dstImage);  
    cvNamedWindow("dstImage",CV_WINDOW_AUTOSIZE);
    cvShowImage("dstImage",dstImage);   
    on_change1(pos1);
    on_change2(pos2);
    on_change3(pos3);
    on_change4(pos4);
    on_change5(pos5);
    on_change6(pos6);
    cvCreateTrackbar("H_min","dstImage",&pos1,360,on_change1);
    cvCreateTrackbar("H_max","dstImage",&pos4,360,on_change4);
    cvCreateTrackbar("S_min","dstImage",&pos2,255,on_change2);
    cvCreateTrackbar("S_max","dstImage",&pos5,255,on_change5);
    cvCreateTrackbar("V_min","dstImage",&pos3,255,on_change3);
    cvCreateTrackbar("V_max","dstImage",&pos6,255,on_change6);
    for(y=0;y<srcImage->height;y++)
        for(x=0;x<srcImage->width;x++)
        {   
            r=cvGetReal2D(srcR,y,x);
            g=cvGetReal2D(srcG,y,x);
            b=cvGetReal2D(srcB,y,x);
            cvSetReal2D(dstR,y,x,r);
            cvSetReal2D(dstG,y,x,g);
            cvSetReal2D(dstB,y,x,b);
        }   
    cvSetZero(dstImage);
    cvMerge(dstB,dstG,dstR,NULL,dstImage);
    cvShowImage("dstImage",dstImage);
    cvWaitKey(0);
    cvDestroyWindow("srcImage");
    cvDestroyWindow("dstImage");
    cvReleaseImage(&srcImage);  
    cvReleaseImage(&srcR);
    cvReleaseImage(&srcG);
    cvReleaseImage(&srcB);
    cvReleaseImage(&dstImage);
}

And The code that i've tried

    enter code here


using namespace std;
using namespace cv;
 Mat src,g,r,b, fin_img;
 int H_max,H_min,V_max,V_min,S_max,S_min;
 void RGB_TO_HSV(unsigned char r, unsigned char g, unsigned char b, double &h, double &s, double &v);
void HSV_TO_RGB(unsigned char h, unsigned char s, unsigned char v, double &r, double &g, double &b);
void FILTER(int H_max,int H_min,int V_max,int V_min,int S_max,int S_min);
void on_change1(int pos1=0);
void on_change2(int pos2=0);
void on_change3(int poS3=0);
void on_change4(int poS4=0);
void on_change5(int poS5=0);
void on_change6(int poS6=0);
 vector<Mat> rgbChannels(3);
 vector<Mat> NewrgbChannels(3);
 vector<Mat> channels;
 vector<Mat> Newchannels;
int main()
{
    //double r, g, b, h, s, v;
    int x,y;
    int pos1=0;
    int pos2=0;
    int pos3=0;
    int pos4=360;
    int pos5=255;
    int pos6=255;
    src=imread("sunrise.jpg",1);    
    namedWindow("src",1);imshow("src",src);


    rgbChannels[0] = src(Rect(0,0,src.size().width,src.size().height));
    rgbChannels[1] = src(Rect(0,0,src.size().width,src.size().height));
    rgbChannels[2] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[0] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[1] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[2] = src(Rect(0,0,src.size().width,src.size().height));

    fin_img = src(Rect(0,0,src.size().width,src.size().height));
    // Split the image into different channels

   split(src, rgbChannels);

    g = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
     r = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
     b = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);

    {
    // For Merge
    channels.push_back(rgbChannels[0]); // B
    channels.push_back(rgbChannels[1]); // G
    channels.push_back(rgbChannels[2]); //R


    merge(channels, fin_img); /// Merge the three channels
    namedWindow("dstImage",1);imshow("dstImage", fin_img);
    }
    on_change1(pos1);
    on_change2(pos2);
    on_change3(pos3);
    on_change4(pos4);
    on_change5(pos5);
    on_change6(pos6);
    createTrackbar("H_min","dstImage",&pos1,360,TrackbarCallback(on_change1),0);
    createTrackbar("H_max","dstImage",&pos4,360,TrackbarCallback(on_change4),0);
    createTrackbar("S_min","dstImage",&pos2,255,TrackbarCallback(on_change2),0);
    createTrackbar("S_max","dstImage",&pos5,255,TrackbarCallback(on_change5),0);
    createTrackbar("V_min","dstImage",&pos3,255,TrackbarCallback(on_change3),0);
    createTrackbar("V_max","dstImage",&pos6,255,TrackbarCallback(on_change6),0);

    for(y=0;y<src.cols;y++)
        for(x=0;x<src.rows;x++)
        {    

        r= rgbChannels[2].data[ x*rgbChannels[2].rows+ y*rgbChannels[2].cols];
        g= rgbChannels[1].data[ x*rgbChannels[1].rows+ y*rgbChannels[1].cols];
        b= rgbChannels[0].data[ x*rgbChannels[0].rows+ y*rgbChannels[0].cols];
        NewrgbChannels[2].data[ NewrgbChannels[2].rows+ NewrgbChannels[2].cols] = rgbChannels[2].data[ x*rgbChannels[2].rows+ y*rgbChannels[2].cols]; 
        NewrgbChannels[1].data[ NewrgbChannels[1].rows+ NewrgbChannels[1].cols] = rgbChannels[1].data[ x*rgbChannels[1].rows+ y*rgbChannels[1].cols];
        NewrgbChannels[0].data[ NewrgbChannels[0].rows+ NewrgbChannels[0].cols] = rgbChannels[0].data[ x*rgbChannels[0].rows+ y*rgbChannels[0].cols];
        }
        Newchannels.push_back(NewrgbChannels[0]); // B
    Newchannels.push_back(NewrgbChannels[1]); // G
    Newchannels.push_back(NewrgbChannels[2]); //R
         merge(Newchannels, fin_img);
     imshow("dstImage", fin_img);

    waitKey(0);

}

How to convert my code of IplImage to cv::Mat or How to make my code by using cv::Mat?

I was trying to convert Iplimage structure to cv::Mat class. But I got something wrong error in new code that I fixed.

So, I just want to compare what you guys will fix it with mine. Can you convert my code by using cv::Mat class?

Here is my code that has only IplImage

enter code here
#include<opencv2/core/core_c.h>
#include<opencv2/highgui/highgui_c.h>
#include<opencv/cv.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include<Windows.h>
#include<ctffunc.h> 
void RGB_TO_HSV(unsigned char r, unsigned char g, unsigned char b, double &h, double &s, double &v );
void HSV_TO_RGB(unsigned char h, unsigned char s, unsigned char v, double &r, double &g, double &b);
void FILTER(int H_max,int H_min,int V_max,int V_min,int S_max,int S_min);
void on_change1(int pos1);
void on_change2(int pos2);
void on_change3(int poS3);
void on_change4(int poS4);
void on_change5(int poS5);
void on_change6(int poS6);
IplImage *srcImage,*dstImage,*srcR,*srcG,*srcB,*dstR,*dstG,*dstB;
int H_max,H_min,V_max,V_min,S_max,S_min;
int main()
{   double r, g, b, h, s, v;
    int x,y;
    int pos1=0;
    int pos2=0;
    int pos3=0;
    int pos4=360;
    int pos5=255;
    int pos6=255;   
    if((srcImage=cvLoadImage("sunrise.jpg",1))==NULL)
        return -1;
    srcR=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    srcG=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    srcB=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstR=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstG=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
    dstB=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1); 
    cvSplit(srcImage,srcB,srcG,srcR,NULL);  
    dstImage=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,3);
    cvSetZero(dstImage);
    cvMerge(srcB,srcG,srcR,NULL,dstImage);  
    cvNamedWindow("dstImage",CV_WINDOW_AUTOSIZE);
    cvShowImage("dstImage",dstImage);   
    on_change1(pos1);
    on_change2(pos2);
    on_change3(pos3);
    on_change4(pos4);
    on_change5(pos5);
    on_change6(pos6);
    cvCreateTrackbar("H_min","dstImage",&pos1,360,on_change1);
    cvCreateTrackbar("H_max","dstImage",&pos4,360,on_change4);
    cvCreateTrackbar("S_min","dstImage",&pos2,255,on_change2);
    cvCreateTrackbar("S_max","dstImage",&pos5,255,on_change5);
    cvCreateTrackbar("V_min","dstImage",&pos3,255,on_change3);
    cvCreateTrackbar("V_max","dstImage",&pos6,255,on_change6);
    for(y=0;y<srcImage->height;y++)
        for(x=0;x<srcImage->width;x++)
        {   
            r=cvGetReal2D(srcR,y,x);
            g=cvGetReal2D(srcG,y,x);
            b=cvGetReal2D(srcB,y,x);
            cvSetReal2D(dstR,y,x,r);
            cvSetReal2D(dstG,y,x,g);
            cvSetReal2D(dstB,y,x,b);
        }   
    cvSetZero(dstImage);
    cvMerge(dstB,dstG,dstR,NULL,dstImage);
    cvShowImage("dstImage",dstImage);
    cvWaitKey(0);
    cvDestroyWindow("srcImage");
    cvDestroyWindow("dstImage");
    cvReleaseImage(&srcImage);  
    cvReleaseImage(&srcR);
    cvReleaseImage(&srcG);
    cvReleaseImage(&srcB);
    cvReleaseImage(&dstImage);
}

And The code that i've tried

    enter code here


using namespace std;
using namespace cv;
 Mat src,g,r,b, fin_img;
 int H_max,H_min,V_max,V_min,S_max,S_min;
 void RGB_TO_HSV(unsigned char r, unsigned char g, unsigned char b, double &h, double &s, double &v);
void HSV_TO_RGB(unsigned char h, unsigned char s, unsigned char v, double &r, double &g, double &b);
void FILTER(int H_max,int H_min,int V_max,int V_min,int S_max,int S_min);
void on_change1(int pos1=0);
void on_change2(int pos2=0);
void on_change3(int poS3=0);
void on_change4(int poS4=0);
void on_change5(int poS5=0);
void on_change6(int poS6=0);
 vector<Mat> rgbChannels(3);
 vector<Mat> NewrgbChannels(3);
 vector<Mat> channels;
 vector<Mat> Newchannels;
int main()
{
    //double r, g, b, h, s, v;
    int x,y;
    int pos1=0;
    int pos2=0;
    int pos3=0;
    int pos4=360;
    int pos5=255;
    int pos6=255;
    src=imread("sunrise.jpg",1);    
    namedWindow("src",1);imshow("src",src);


    rgbChannels[0] = src(Rect(0,0,src.size().width,src.size().height));
    rgbChannels[1] = src(Rect(0,0,src.size().width,src.size().height));
    rgbChannels[2] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[0] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[1] = src(Rect(0,0,src.size().width,src.size().height));
    NewrgbChannels[2] = src(Rect(0,0,src.size().width,src.size().height));

    fin_img = src(Rect(0,0,src.size().width,src.size().height));
    // Split the image into different channels

   split(src, rgbChannels);

    g = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
     r = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
     b = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);

    {
    // For Merge
    channels.push_back(rgbChannels[0]); // B
    channels.push_back(rgbChannels[1]); // G
    channels.push_back(rgbChannels[2]); //R


    merge(channels, fin_img); /// Merge the three channels
    namedWindow("dstImage",1);imshow("dstImage", fin_img);
    }
    on_change1(pos1);
    on_change2(pos2);
    on_change3(pos3);
    on_change4(pos4);
    on_change5(pos5);
    on_change6(pos6);
    createTrackbar("H_min","dstImage",&pos1,360,TrackbarCallback(on_change1),0);
    createTrackbar("H_max","dstImage",&pos4,360,TrackbarCallback(on_change4),0);
    createTrackbar("S_min","dstImage",&pos2,255,TrackbarCallback(on_change2),0);
    createTrackbar("S_max","dstImage",&pos5,255,TrackbarCallback(on_change5),0);
    createTrackbar("V_min","dstImage",&pos3,255,TrackbarCallback(on_change3),0);
    createTrackbar("V_max","dstImage",&pos6,255,TrackbarCallback(on_change6),0);

    for(y=0;y<src.cols;y++)
        for(x=0;x<src.rows;x++)
        {    

        r= rgbChannels[2].data[ x*rgbChannels[2].rows+ y*rgbChannels[2].cols];
        g= rgbChannels[1].data[ x*rgbChannels[1].rows+ y*rgbChannels[1].cols];
        b= rgbChannels[0].data[ x*rgbChannels[0].rows+ y*rgbChannels[0].cols];
        NewrgbChannels[2].data[ NewrgbChannels[2].rows+ NewrgbChannels[2].cols] = rgbChannels[2].data[ x*rgbChannels[2].rows+ y*rgbChannels[2].cols]; 
        NewrgbChannels[1].data[ NewrgbChannels[1].rows+ NewrgbChannels[1].cols] = rgbChannels[1].data[ x*rgbChannels[1].rows+ y*rgbChannels[1].cols];
        NewrgbChannels[0].data[ NewrgbChannels[0].rows+ NewrgbChannels[0].cols] = rgbChannels[0].data[ x*rgbChannels[0].rows+ y*rgbChannels[0].cols];
        }
        Newchannels.push_back(NewrgbChannels[0]); // B
    Newchannels.push_back(NewrgbChannels[1]); // G
    Newchannels.push_back(NewrgbChannels[2]); //R
         merge(Newchannels, fin_img);
     imshow("dstImage", fin_img);

    waitKey(0);

}