0

Hello,

I am working with images and I have got a problem. I have this method and I want it to return an array of unsigned ints. I have not got problems of compilation but it does not work properly.
Could anyone help me?

Thank you in advanced.

unsigned int* RegionToArray(System::Drawing::Bitmap^ binaryImage,unsigned int coordinate_x, unsigned int coordinate_y, unsigned int width_rect, unsigned int height_rect){
		
		coordinate_x=this->coordinate_x;
		coordinate_y=this->coordinate_y;
		width_rect=this->width_rect;
		height_rect=this->height_rect;
		unsigned int cont=0;
		unsigned int *regionArray;
		regionArray=new unsigned int[height_rect*width_rect];
		

		for(unsigned int i=coordinate_x;i<height_rect;i++)
		{
			for(unsigned int j=coordinate_y;j<width_rect;j++)
			{
				System::Drawing::Color^ colour;
				colour=gcnew System::Drawing::Color();
				colour=binaryImage->GetPixel(i,j);
				unsigned int col;
				col=colour->ToArgb(); 
					
				regionArray[cont]=col;
				cont++;
			}
		}

	return regionArray;
}
6
Contributors
12
Replies
13
Views
8 Years
Discussion Span
Last Post by Duoas
Featured Replies
  • 1
    Duoas 1,025   8 Years Ago

    Depending on how the data is packed. If it is packed (MSB...LSB) ARGB then [code=C++] int alpha = (color >> (3 * 8)) & 0xFF; int red = (color >> (2 * 8)) & 0xFF; int green = (color >> (1 * 8)) & 0xFF; int blue = (color >> … Read More

  • 1
    Duoas 1,025   8 Years Ago

    Actually it does have it backwards... [inlinecode]foo = [B]this[/B]->foo;[/inlinecode] != [inlinecode][B]this[/B]->foo = foo;[/inlinecode] ? Read More

0

I expected that the function returned a value between 0 und 255 (colours) but it return in the first iteration 4261281277 and I do not know why. I have tried to initialize arrayRegion like this

for(unsigned int i=0;i<height_rect*width_rect;i++)
		{
			regionArray[i]=0;
		}

but the problem still remains. Have you got any idea?

0

put a print statement at the beginning of the function to print out the value of height_rect and width_rect. Are they what you would expect? If not then the problem is elsewhere in your program.

0

And you aren't printing regionArray like this : cout << regionArray; right?
Because it would only print the address of the first element of the array.

you should use cout << regionArray[0];

0

ToArgb() returns the combined Alpha/Red/Green/Blue value packed in 32 bits, so if you are expecting it to return values in range 0..255 then you have misunderstood that function.

0

You are rigth, ToArgb() does not return the value of the color.
Do you know how could I make it?

0

You are rigth, ToArgb() does not return the value of the color.
Do you know how could I make it?

The Color has the respective R, G and B members which you can access directly.

1

Depending on how the data is packed.

If it is packed (MSB...LSB) ARGB then

int alpha = (color >> (3 * 8)) & 0xFF;
int red   = (color >> (2 * 8)) & 0xFF;
int green = (color >> (1 * 8)) & 0xFF;
int blue  = (color >> (0 * 8)) & 0xFF;

If it is packed in the Windows order: ABGR

int alpha = (color >> (3 * 8)) & 0xFF;
int blue  = (color >> (2 * 8)) & 0xFF;
int green = (color >> (1 * 8)) & 0xFF;
int red   = (color >> (0 * 8)) & 0xFF;

etc.

You can put these into a function for each component.

Or better yet, an ARGB_t class. You might find my response in this thread at cplusplus.com useful for some ideas. (It would be very simple to extend the RGB_t class to an ARGB_t or ABGR_t or whatever you need class.)

Hope this helps.

Votes + Comments
Great link.
0

I am not an expert but i am trying to learn .

I dont understand something in the program.

&rea 's code

unsigned int* RegionToArray(System::Drawing::Bitmap^ binaryImage,unsigned int coordinate_x, unsigned int coordinate_y, unsigned int width_rect, unsigned int height_rect){
coordinate_x=this->coordinate_x;
coordinate_y=this->coordinate_y;
width_rect=this->width_rect;
height_rect=this->height_rect;

Well If the function is a class member i dont understand why &rea used all the variables as Arguements when they can be accessed using this ->

0

I am not an expert but i am trying to learn .

I dont understand something in the program.

&rea 's code

unsigned int* RegionToArray(System::Drawing::Bitmap^ binaryImage,unsigned int coordinate_x, unsigned int coordinate_y, unsigned int width_rect, unsigned int height_rect){
coordinate_x=this->coordinate_x;
coordinate_y=this->coordinate_y;
width_rect=this->width_rect;
height_rect=this->height_rect;

Well If the function is a class member i dont understand why &rea used all the variables as Arguements when they can be accessed using this ->

The function parameters coordinate_x, coordinate_y. width_rect, and height_rect are not class members but are passed into the function by somethig else, which was not posted. The statement you quoted is just copying them into the class variables.

1

Actually it does have it backwards... foo = [B]this[/B]->foo; != [B]this[/B]->foo = foo; ?

Votes + Comments
you are right, I should have seen that too
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.