Ask Your Question
0

Nested if-else conditions

asked 2016-12-11 23:23:47 -0600

VPallavi gravatar image

Hello All...I know this is very small and stupid doubt but I don't know why I'm getting stuck here. I am using nested if-else loops inside "for" loop in my code but it at all not going inside this "for" loop. I checked it by putting Log.d statements inside each condition but none of then are getting printed. My block of code is as follows:

         Mat fin = new Mat (gray.rows(), gray.cols(), CvType.CV_8UC3);
         for (int ii=0;ii<rows;ii++)
        {
            for (int j=0;j<cols;j++)
            {
                if ( gray.get(ii, j)[0] < 32)
                {
                    fin.get(ii, j)[0] = 1;
                }
                else if (gray.get(ii, j)[0]>31 && gray.get(ii, j)[0] < 64)
                {
                    fin.get(ii, j)[0] = 2;
                }
                else if (gray.get(ii, j)[0] > 63 && gray.get(ii, j)[0] < 96)
                {
                    fin.get(ii, j)[0] = 3;
                }
                else if (gray.get(ii, j)[0] > 95 && gray.get(ii, j)[0] < 128)
                {
                    fin.get(ii, j)[0] = 4;
                }
                else if (gray.get(ii, j)[0] > 127 && gray.get(ii, j)[0] < 160)
                {
                    fin.get(ii, j)[0] = 5;
                }
                else if (gray.get(ii, j)[0] > 159 && gray.get(ii, j)[0] < 192)
                {
                    fin.get(ii, j)[0] = 6;
                }
                else if (gray.get(ii, j)[0] > 191 && gray.get(ii, j)[0] < 224)
                {
                    fin.get(ii, j)[0] = 7;
                }
                else if (gray.get(ii, j)[0] > 223 && gray.get(ii, j)[0] < 256)
                {
                    fin.get(ii, j)[0] = 8;
                }
                     }
            }

Please help!! Thanks in advance!!

edit retag flag offensive close merge delete

Comments

2

may i know what you are trying to achieve here? May be there is no need to loop through pixels!

Balaji R gravatar imageBalaji R ( 2016-12-12 00:16:46 -0600 )edit

Hello, first where you find get() method for cv::Mat class? It is not existed in official docs. Second thing is if conditions - they are overloaded, see if( x < 32) else if( x > 31 && x < 64) is equivalent of if( x < 32) else if( x < 64), but last one demands less computations

pi-null-mezon gravatar imagepi-null-mezon ( 2016-12-12 00:40:15 -0600 )edit

may be something like this :

gray=fin/32; // but only with plane 0
LBerger gravatar imageLBerger ( 2016-12-12 00:43:04 -0600 )edit

@Balaji R:: I am trying to divide 256 pixels values into 8 bins. Just like normalization.

VPallavi gravatar imageVPallavi ( 2016-12-12 00:58:34 -0600 )edit

@pi-null-mezon:: This get() method is used for fetching the respective location. I got it from this forum only earlier.

VPallavi gravatar imageVPallavi ( 2016-12-12 01:02:04 -0600 )edit

@LBerger:: I tried with this earlier but getting all values as zero.

                  fin.get(ii,j)[0]=(gray.get(ii,j)[0]/32)+1;
VPallavi gravatar imageVPallavi ( 2016-12-12 01:07:09 -0600 )edit

I don't know java but you have to split plane first and merge after. in C++ :

vector<Mat> plane;
split(fin,plane)
plane[0]=gray/32;
merge(plane,fin);

it is not really optimized because you split a mat with zero.

Please could you answer to @Balaji R ?

LBerger gravatar imageLBerger ( 2016-12-12 01:16:56 -0600 )edit

@pi-null-mezon:: The second one is also not working.

VPallavi gravatar imageVPallavi ( 2016-12-12 01:19:05 -0600 )edit

You really need to read this "how_to_scan_images"

pi-null-mezon gravatar imagepi-null-mezon ( 2016-12-12 03:16:08 -0600 )edit

You really need to read this how_to_scan_images

pi-null-mezon gravatar imagepi-null-mezon ( 2016-12-12 03:16:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-12 04:47:31 -0600

updated 2016-12-12 04:50:44 -0600

As mentioned in the comments it is recommended to use LUT for this purpose! Here is a Sample implementation Hope This helps!

  #include "stdafx.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{    
    Mat mSrc_Bgr(5, 5, CV_8UC3),mDst_Bgr;    
    randu(mSrc_Bgr, Scalar::all(0), Scalar::all(255));

    cout << "Input Mat = " << endl << format(mSrc_Bgr,3) << endl << endl;


    Mat mLUT(1, 256, CV_8UC3);
    for (int i=0; i<256; i++)
    {
        mLUT.at<Vec3b>(i)[0]= 256/32;       // Blue channel  (B)= Val /32
        mLUT.at<Vec3b>(i)[1]= i;            // Green channel  No Change
        mLUT.at<Vec3b>(i)[2]= i;            // Red channel  No Change
    }

    LUT(mSrc_Bgr,mLUT,mDst_Bgr);

    cout << "Output Mat = " << endl << format(mDst_Bgr,3) << endl << endl;

    return 0;
}
edit flag offensive delete link more

Comments

@Balaji R:: Actually I'm writing code in android using OpenCV library. In C++ I wrote the following code and it's working fine.

          `Mat fin = new Mat (gray.size(),CvType.CV_8UC3, new Scalar(0,0,0));
                         for (int ii=0;ii<rows;ii++)
                     {
                         for (int j=0;j<cols;j++)
                             {
                              fin.get(ii,j)[0]=(gray.get(ii,j)[0]/32)+1;
                                  Log.d("gray value:: ", "" +gray.get(ii,j)[0]);
                                      }
                     }`

But I'm unable to get correct result in android. Can you please help me in this regard. Thanks!

VPallavi gravatar imageVPallavi ( 2016-12-12 05:00:36 -0600 )edit

May i know which part you are not able to reproduce? Can you show us your code?

Balaji R gravatar imageBalaji R ( 2016-12-12 05:13:48 -0600 )edit

@Balaji R:: The code is same only that I posted in question. The point where I'm stuck is that inside the second for loop it is not going under any if-else condition. I put Log.d statements under each if-else but not a single statement is printed. To be more precise, I'm trying to construct GLCM(Gray Level Co-occurance Matrix) with the pixels of "gray" image (Mat type variable). For doing this I need to divide 0-255 into 8 bins by alloting 0-31 as 1, 32-63 as 2,....and so on. I hope it is clear now.

VPallavi gravatar imageVPallavi ( 2016-12-12 07:40:59 -0600 )edit

Please first try to get and set the Matrix values successfully and comeback again if you have any doubts in that!

Balaji R gravatar imageBalaji R ( 2016-12-12 20:26:26 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-12-11 23:23:47 -0600

Seen: 1,764 times

Last updated: Dec 12 '16