Page 1 of 1

What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T00:10:01-07:00
by Airon65
Now I use following approach for accessing to image pixel colors:

Code: Select all

unsigned short _imgWidth = 1920, _imgHeight = 1080;

Magick::Image *image = new Magick::Image();
image->size( Magick::Geometry( _imgWidth, _imgHeight ) );
image->backgroundColor( Magick::Color( "white" ) );
image->erase();

for ( unsigned int j = 0; j < _imgHeight; j++ ) {
	for ( unsigned int i = 0; i < _imgWidth; i++ ) {
		Magick::Color c = image->pixelColor( i, j );
		unsigned char r = c.quantumRed()	* 255 / QuantumRange;
		unsigned char g = c.quantumGreen()	* 255 / QuantumRange;
		unsigned char b = c.quantumBlue()	* 255 / QuantumRange;		
	}	
}

delete image;
I should to process about 5000 images like that. It takes me about 3-4 seconds to just get all the pixels of one image (1920x1080). Are there any methods which work a little bit faster than that? :)

I use ImageMagick 7.0.5-2 Q16 x86_64 2017-03-11.

Re: What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T06:37:50-07:00
by magick
You likely want to get one row of pixels at a time with:

Code: Select all

  Pixels view(image);
  for ( unsigned int j = 0; j < _imgHeight; j++ ) {
    Quantum *pixels = view.getConst(0,y,_imgWidth,1)
    for ( unsigned int i = 0; i < _imgWidth; i++ ) {
      unsigned char r = 255*QuantumScale**pixels++;
      unsigned char g = 255*QuantumScale**pixels++;
      unsigned char b = 255*QuantumScale**pixels++;
    }
  

Re: What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T10:17:48-07:00
by Airon65
Thanks! That approach works really fast!

Re: What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T10:39:50-07:00
by dlemstra
I would advise you to do some more reading on how to code C++. The error that you are receiving should not be that difficult to figure out. This is an error that is not related to Magick++.

Re: What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T11:13:51-07:00
by Airon65
Is that right that in case of my RGB image:

Code: Select all

Magick::Image *image = new Magick::Image();
image->size( Magick::Geometry( _imgWidth, _imgHeight ) );
image->backgroundColor( Magick::Color( "white" ) );
image->erase();
I get 4 bytes per pixel:

Code: Select all

Magick::Pixels view( *image );
for ( unsigned int j = 0; j < _imgHeight; j++ ) {
	const Magick::Quantum *pixels = view.getConst( 0, j, _imgWidth, 1 );
	for ( unsigned int i = 0; i < _imgWidth; i++ ) {
		unsigned int r = 255 * QuantumScale * *( pixels + ( i * 4 ) + 0 );
		unsigned int g = 255 * QuantumScale * *( pixels + ( i * 4 ) + 1 );
		unsigned int b = 255 * QuantumScale * *( pixels + ( i * 4 ) + 2 );
	}	
}
if I set: "( i * 3 )" - I get a broken image. I didn't why I try to test "i*4" but it worked for my as I thought RGB (3 bytes per pixel image).

Re: What's the fastest method for accessing to pixel color?

Posted: 2017-04-02T14:34:56-07:00
by magick
Use image->channels() to determine how many channels you have. If its four add an alpha channel.