Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C++
#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);
}


What I have tried:

C++
//IplImage *srcImage1,*dstImage1,*srcR1,*srcG1,*srcB1,*dstR1,*dstG1,*dstB1;
Mat 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;	
	 
	srcImage = imread("sunrise.jpg");
	imshow("srcImage",srcImage);
	if(srcImage.empty()){
		return -1;}
/*	 
	Mat srcR=cvarrToMat(srcR1);
	Mat srcG=cvarrToMat(srcG1);
	Mat srcB=cvarrToMat(srcB1);
	Mat dstR=cvarrToMat(dstR1);
	Mat dstG=cvarrToMat(dstG1);
	Mat dstB=cvarrToMat(dstB1);
*/	
	//srcR = srcImage.create(100,60,IPL_DEPTH_8U);
	//srcR = cvCreateImage(cvGetSize(&srcImage),IPL_DEPTH_8U,1);
	srcR = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	srcG = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	srcB = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	dstR = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	dstG = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	dstB = srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));

	cvSplit(&srcImage,&srcB,&srcG,&srcR,NULL);
	dstImage=srcImage(Rect(0,0,srcImage.size().width,srcImage.size().height));
	
	cvSetZero(&dstImage);
	cvMerge(&srcB,&srcG,&srcR,NULL,&dstImage);	
	cvNamedWindow("dstImage",CV_WINDOW_AUTOSIZE);
	imshow("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.size().height;y++)
	for(x=0;x<srcimage.size().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);
	imshow("dstImage",dstImage);
	cvWaitKey(0);

	cvDestroyWindow("srcImage");
	cvDestroyWindow("dstImage");
	/* //cv::destroyAllWindows();
	
	*/cvReleaseImage(&&srcImage);	
	cvReleaseImage(&srcR);
	cvReleaseImage(&srcG);
	cvReleaseImage(&srcB);
	cvReleaseImage(&dstImage);
srcImage.release();
	srcR.release();
	srcG.release();
	srcB.release();
	dstImage.release();
	
}
Posted
Updated 22-Feb-16 20:11pm
v2
Comments
[no name] 22-Feb-16 22:45pm    
Where is the error? What is the problem? What you have presented is not a question.
Member 12269744 23-Feb-16 0:37am    
i got this error
OpenCV Error: Assertion failed (i < 0) in cv::_OutputArray::create, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 2173
[no name] 23-Feb-16 1:06am    
Fine. You have a bug. What did you discover using your debugger? You may refer to this link: https://msdn.microsoft.com/en-us/library/aa293531(v=vs.60).aspx

You may have to do some work to track down where the problem occurs in your code and then you can ask a better question.
Member 12269744 13-Mar-16 20:02pm    
Could you give me an address of email?? then, I'd ask some questions...

1 solution

First thing to do is learn how to use C++ effectively. There's a lot of guff in your code that is redundant - get rid of that (i.e. stop trying to manage memory manually, declare variables as you need 'em and bin the globals) and your problem will be easier to isolate.

The next thing is to start small. Does the image load and display properly? If so does can you save it without processing it. If you can then maybe something in your processing is knackering the image. If not then you've got a deeper problem. And so on until you know what you have to do at a minimum to cause the error. Then check the OpenCV docs or, if the worst comes to the worst, read the source code to see what you're doing wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900