Ask Your Question
-1

Some small problems about OpenCV4Android haze removal

asked 2014-05-06 08:18:19 -0600

maizi gravatar image

updated 2014-05-06 19:48:56 -0600

I am a new of OpenCV4Android.I have a piece of code about OpenCV which written by C.I want to rewrite it in Android and use my cellphone to make it come ture.But some function I don't know why can't call ,for example cvSetImageROI();cvCopy();cvMinMaxLoc();cvSetImageROI();cvSet() etc.Who can teach me how to deal with it.

The whole code is blow
int block=5;
int w1=80;
double w;
IplImage *src=NULL;
IplImage *dst=NULL;


IplImage *quw(IplImage *src,int block,double w)
{

         IplImage *dst1=NULL;
         IplImage *dst2=NULL;
         IplImage *dst3=NULL;
         IplImage *imgroi1;

         IplImage *imgroi2;

         IplImage *imgroi3;

         IplImage *roidark;

         IplImage *dark_channel=NULL;

         IplImage *toushelv=NULL;
         IplImage *j1=NULL;
         IplImage *j2=NULL;
         IplImage *j3=NULL;

         IplImage *dst=NULL;
         CvRect ROI_rect;


         dst1=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
         dst2=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
         dst3=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);


         imgroi1=cvCreateImage(cvSize(block,block),IPL_DEPTH_8U,1);
         imgroi2=cvCreateImage(cvSize(block,block),IPL_DEPTH_8U,1);
         imgroi3=cvCreateImage(cvSize(block,block),IPL_DEPTH_8U,1);
         roidark=cvCreateImage(cvSize(block,block),IPL_DEPTH_8U,1);


         j1=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
         j2=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
         j3=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);

         dark_channel=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);

         toushelv=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);

         dst=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,3);

         cvSplit(src,dst1,dst2,dst3,NULL);
         ROI_rect.width=block;
         ROI_rect.height=block;
         ROI_rect.x=0;
         ROI_rect.y=0;


         int i;
         int j;
         double min1=0;
         double max1=0;
         double min2=0;
         double max2=0;
         double min3=0;
         double max3=0;
         double min=0;
         CvScalar value;
         for(i=0;i<src->width/block;i++)
         {  for(j=0;j<src->height/block;j++)
                   {

                     cvSetImageROI(dst1,ROI_rect);
                     cvCopy(dst1,imgroi1,NULL);
                     cvMinMaxLoc(imgroi1,&min1,&max1,NULL,NULL);
                     cvSetImageROI(dst2,ROI_rect);
                     cvCopy(dst2,imgroi2,NULL);
                     cvMinMaxLoc(imgroi2,&min2,&max2,NULL,NULL);
                     cvSetImageROI(dst3,ROI_rect);
                     cvCopy(dst3,imgroi3,NULL);
                     cvMinMaxLoc(imgroi3,&min3,&max3,NULL,NULL);

                     if(min1<min2)
                        min=min1;
                     else
                     min=min2;
                     if(min>min3)
                     min=min3;
                     alar(min,min,min,min);

                    cvSetImageROI(dark_channel,ROI_rect);
                    cvSet(roidark,value,NULL);
                     cvCopy(roidark,dark_channel,NULL);

                    cvResetImageROI(dst1);
                    cvResetImageROI(dst2);
                    cvResetImageROI(dst3);
                    cvResetImageROI(dark_channel);

                    ROI_rect.x=block*i;
                    ROI_rect.y=block*j;
                   }
         }

         cvSaveImage("f:/dark_channel_prior.jpg",dark_channel);

         double min_dark;
         double max_dark;
         CvPoint min_loc;
         CvPoint max_loc;          

         cvMinMaxLoc(dark_channel,&min_dark,&max_dark,&min_loc,&max_loc,NULL);
         cout<<max_loc.x<<" "<<max_loc.y<<endl;
         ROI_rect.x=max_loc.x;
         ROI_rect.y=max_loc.y;
         double A_dst1;
         double dst1_min;
         double A_dst2;
         double dst2_min;
         double A_dst3;
         double dst3_min;
         cvSetImageROI(dst1,ROI_rect);

         cvCopy(dst1,imgroi1,NULL);
         cvMinMaxLoc(imgroi1,&dst1_min,&A_dst1,NULL,NULL);
         cvSetImageROI(dst2,ROI_rect);
         cvCopy(dst2,imgroi2,NULL);
         cvMinMaxLoc(imgroi2,&dst2_min,&A_dst2,NULL,NULL);
         cvSetImageROI(dst3,ROI_rect);
         cvCopy(dst3,imgroi3,NULL);
         cvMinMaxLoc(imgroi3,&dst3_min,&A_dst3,NULL,NULL);
         cout<<A_dst1<<" "<<A_dst2<<" "<<A_dst3<<endl;

         int k;
         int l;
         CvScalar m;
         CvScalar n;

        for(k=0;k<src->height;k++)
         {for(l=0;l<src->width;l++)
            {
              m=cvGet2D(dark_channel,k,l);
              n=cvScalar(255-w ...
(more)
edit retag flag offensive close merge delete

Comments

Please sir ...

 1. This is an English forum, having chinese comments in your code doesn't help
 2. Your explanation is not clear on what you are expecting exactly...
 3. You are using an old C-API, please switch to the C++-API variant
StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-06 08:35:58 -0600 )edit

Thank you for your comments. My main question I don't know how to switch the code to C++ API,because my English is not good enough to read the official documents.

maizi gravatar imagemaizi ( 2014-05-06 20:00:54 -0600 )edit

Let me give you a hand! Just a sec please.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-07 02:19:07 -0600 )edit

"The whole code is blow" - well, indeed ;(

berak gravatar imageberak ( 2016-02-20 13:54:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-07 03:45:15 -0600

updated 2014-05-07 03:46:48 -0600

Here is the C++ variant. However go through it and please take a look at all the comments I made. As you see, you are making it way to complex using the C-API.

// -------------------------------------------------------
// Added all includes and namespaces needed for this code
// Add it in correct place of your program
// -------------------------------------------------------
#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;
// -------------------------------------------------------

int block=5;
int w1=80;
double w;
Mat src, dst;

Mat quw(Mat src, int block, double w)
{
    // Tried to order the variables in meaningfull groups
    // Copied them to the place they are actually used
    // Removed B G R channel variables and replaced them by vectors
    // Cleaned up the code    

    // Split the source in channels
    // You can now address them by index!
    vector<Mat> channels;
    split(src, channels);

    // Set a region of interest rect
    Rect ROI_rect ( Point(0,0), Size(block, block) );

    // Parameters needed after the looping also 
    Mat imgroi1 (block, block, CV_8UC1); 
    Mat imgroi2 (block, block, CV_8UC1);
    Mat imgroi3 (block, block, CV_8UC1);

    // Start your for loop, counters don't need to be initialized beforehand
    for(int i=0; i<src.cols/block; i++){
       for(int j=0; j<src.rows/block; j++){
       double min1, max1, min2, max2, min3, max3, min;

       // Copy of data to the destination matrices based on the ROI parameter
       // First grab the ROI of the channel, then copy it
           Mat roi1(channels[0], ROI_rect); roi1.copyTo(imgroi1);
           Mat roi2(channels[0], ROI_rect); roi2.copyTo(imgroi2);
           Mat roi3(channels[0], ROI_rect); roi3.copyTo(imgroi3);

       // Apply the min max localisation algorithm on each channel roi
           minMaxLoc(imgroi1, min1, max1);
       minMaxLoc(imgroi2, min2, max2);
       minMaxLoc(imgroi3, min3, max3);

       // Perform the if loops
           if(min1<min2){
        min=min1;
       }else{
        min=min2;
       }
       if(min>min3){
        min=min3;
       }
       // I think a piece of code was missing here, guessing you want a scalar
       // I created one, 4th element is not used since it is the alfa channel for opacity
       // But it seems it isn't used, remove it then
           Scalar minScalar(min,min,min);

       // Add the pixels to the dark channel
           Mat dark_channel = Mat::zeros(src.cols, src.rows, CV_8UC1);
       Mat roi_dark_channel = dark_channel(ROI_rect);
       // Then you are again doing some weird stuff which makes no sense
       // Is your goal here to color the min value that is returned into the dark_channel or so?
       // I will do that here --> setting all pixels of the mask into the lowest retrieved value, seems logical
       roi_dark_channel = min;

       // Adapt the ROI parameter
       ROI_rect.x = block*i;
           ROI_rect.y = block*j;        
       }
    }

    // Write away the image
    // It will contain at this point a matrix with for every block the minimal value
    imwrite("f:/dark_channel_prior.jpg", dark_channel);

    // Now some calculations on the dark_channel image are performed
    double min_dark, max_dark;
    Point min_loc, max_loc;          
    minMaxLoc(dark_channel, min_dark, max_dark, min_loc, max_loc);

    // Output the result and set the ROI with that
    cout<< max_loc.x << " " << max_loc.y << endl;
    ROI_rect.x=max_loc.x;
    ROI_rect.y=max_loc.y;

    // Lets perform some operations using the above data to create the output dst image      
    // Create the destination ...
(more)
edit flag offensive delete link more

Comments

I am sorry for the identation .. it was ok on my system, but not here. Don't have time now to adapt it.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-07 03:48:04 -0600 )edit

Thank you very much! your comments in the code is right. sorry for the "toushelv" , it is chinese words it means transmittance. I don't remeber the word in Enghish,so~~~.But I am learning OpenCV4Android,some function and parameter can't find or call,like Vector<Mat>channels; Mat roi_dark_channel = dark_channel(ROI_rect); I use this inplace of your code: Rect ROI_rect = null; Mat roi_dark=new Mat();
Mat dark_channel =new Mat();
Mat toushelv =new Mat();
Utils.bitmapToMat(bmp, src);
Core.split(src, dst);
Mat dstR=dst.get(0); Mat dstG=dst.get(0); Mat dstB=dst.get(0); Do you know OpenCV4Android,it maks me crazy

maizi gravatar imagemaizi ( 2014-05-14 07:55:30 -0600 )edit

No I don't.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-14 08:30:10 -0600 )edit

Question Tools

Stats

Asked: 2014-05-06 08:18:19 -0600

Seen: 1,063 times

Last updated: May 07 '14