Pierre
Pierre

Reputation: 1174

How to extract a subimage from a 1-dimension array?

I have an image represented by a 1-dimension array (char[]). Image height is H, image width is W, and I would like to extract a subimage starting in (dx,dy) and which dimensions are (dW,dH)

This seems not to work :

subimage(i,j) = image[(j+dy*W) * (W+i+dx)]

Can somebody help?

Upvotes: 1

Views: 78

Answers (1)

samgak
samgak

Reputation: 24427

The formula for a particular pixel in an image stored a 1-dimensional array with the stride equal to the image width is:

pixel(x,y) = image[(y * width) + x]

So the formula you're looking for is (in pseudo-code):

subimage(i,j) = image[((j+dy)*W) + (i+dx)]

Iterate j over 0 to dH and i over 0 to dW.

Upvotes: 1

Related Questions