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

asked 2016-02-25 19:16:46 -0600

DKang gravatar image

updated 2020-10-13 13:52:21 -0600

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 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason not a real question by sturkmen
close date 2020-10-13 13:51:54.759715