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;
}

it does not work properly

Could you be more specific about what it fails to do?

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?

I'm guessing that i or j has an invalid value when calling GetPixel(i,j).

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.

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];

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.

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

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.

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.

commented: Great link. +3

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 ->

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.

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

commented: you are right, I should have seen that too +30
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.