GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Jun-2012, 05:37
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Display pixel values of image


I have this function that displays the values of a binary image

CPP / C++ / C Code:

void accessPixel(Mat& img)
{
    for (int i = 0; i < img.rows; i++)
    {
        for(int j = 0; j < img.cols; j++ )
        {
            if (img.channels() == 1)
            { 
                // gray-level image
                cout << img.at<Vec3s>(j,i)[0] << " ";

                cout << endl;
            }          
        }
    }

}

int main(int argc, char** argv)
{
    int minThreshold = 80;
    int maxThreshold = 255;
    
    Mat dst, src_gray;
    cvtColor(coverImage, src_gray, CV_RGB2GRAY);
    threshold(src_gray, dst, minThreshold, maxThreshold, CV_THRESH_BINARY);
   
    accessPixel(dst);

And since it's binary I thought it will only have two values and will only have 1 channel
The above code displays values -1, -256, 0, 255.

And since it only has 1 channel how come if I change the index [0] to [1] (or to any other number). It's still displays values. I thought It will go out of scope now since it only has 1 channel.
Code:
cout << img.at<Vec3s>(j,i)[0] << " ";

Any help???
Last edited by LuciWiz : 17-Jun-2012 at 14:27. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 17-Jun-2012, 09:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,149
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Display pixel values of image


Quote:
Originally Posted by stbb24
I have this function that displays the values of a binary image

Code:
. . . cvtColor(coverImage, src_gray, CV_RGB2GRAY); . . . cout << img.at<Vec3s>(j,i)[0] << " "; . .

That makes no sense to me. You have converted the image to grayscale (the OpenCV image type is CV_8UC1). The 8-bit pixel values are stored contiguously in an array of unsigned char. What the heck do you think would be accomplished by trying to access them as though they were elements of a 3-D vector of 16-bit integers?

You can access the pixels of a CV_8UC1 image Mat in a number of ways. Perhaps the most convenient way would be
CPP / C++ / C Code:
              // Pixel value of at row i, column j of CV_8UC1 Mat:
                unsigned char uc1 = img.at<unsigned char>(i,j);
              // To see numerical value of unsigned char, cast it to int data type
                cout << (int)uc1

Of course, you don't actually need a separate value if all you are going to to is print out the pixel values:
CPP / C++ / C Code:
//
void printCV_8UC1Pixels(const Mat & img)
{
    cout << "In printCV_8UC1Pixels: img.type() = " << img.type() << endl; // For debugging

    if (img.type() != CV_8UC1)
    {
        cerr << "This function only works with a CV_8UC1 image!" << endl;
        return;
    }

    for (int i = 0; i < img.rows; i++)
    {
        for(int j = 0; j < img.cols; j++ )
        {
            // To print numeric value, cast unsigned char to int
            cout << "Pixel at (row,col) = (" << i << "," << j
                 << "): "
                 << (int)img.at<unsigned char>(i,j) << endl;
        }
    }
}
Quote:
Originally Posted by stbb24
And since it's binary I thought it will only have two values and will only have 1 channel
You have told it to take two successive bytes and make a 16-bit signed integer from them.

Successive byte values might be 0x00,0x00 or 0x00,0xff or 0xff,0x00 or 0xff,0xff
Then, These will result in signed short integer values of 0, -256, 255, and -1,

Quote:
Originally Posted by stbb24
I thought It will go out of scope...
The thing you are dealing with is a pointer. (I don't think it's relevant here exactly what it is pointing to and where it is pointing, since that is not the way to do what you want to do.) "Out of scope" has nothing whatsoever to do with whatever it is pointing to, and, in fact the compiler can not save you from all instances of insanity.



Regards,

Dave
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 17-Jun-2012 at 10:57.
  #3  
Old 17-Jun-2012, 17:33
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Re: Display pixel values of image


Thanks davekw7x it's like your always online
 
 

Recent GIDBlogManaging sshd Brute-force Attacks with iptables by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert existing image to black and white? kkmoslehpour C Programming Language 2 14-Oct-2011 16:19
Displaying Pixel Values Of Bitmap Image bharath1234 C Programming Language 4 27-Mar-2011 21:06
NetBeans - Inventory Program biglez123 Java Forum 1 23-Jan-2011 19:48
Image display problem Griff Web Design Forum 1 23-Aug-2004 17:47
display image on tv monitor Hood Graphics Forum 0 04-May-2004 16:21

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:33.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.