0

For a given image with height = 17, and width = 11.(image A),

Are the following statements equivalent?

In Matlab: halfHeight = round(17/2); (answer = 9)

In C++: int halfHeight = ceil(17/2); (answer = 9)

For pixel access, how can I be sure that I'm accessing the correct pixel values?

Cecilia
  • 4,512
  • 3
  • 32
  • 75
Mzk
  • 1,102
  • 3
  • 17
  • 40
  • To add the above, I've seen some post stated we need to plus 1 for pixel access in C++. Why do we need to plus 1? – Mzk Mar 12 '12 at 10:26
  • Can you tell us a sample code where you want to use it ? – bubble Mar 12 '12 at 10:48
  • In Matlab: [H,W] = size(image); H = round(H/2); half_character = imcrop(image,[0 0 W H]); I want to write it in C++, opencv – Mzk Mar 12 '12 at 11:10
  • matlab is uses base 1 index, C++ uses base 0 index. You may want to subtract 1 when using C++. – Pavan Yalamanchili Mar 12 '12 at 12:17
  • Is this right? IplImage* half_character = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1); half_character = cvCloneImage(image); cvSetImageROI(half_character, cvRect(0, 0, width, halfHeight)); – Mzk Mar 12 '12 at 12:42
  • Is image A supposed to be linked here? – Waynn Lue Mar 12 '12 at 20:31
  • I'm suppose image A is just an example. I wonder if what i do is correct. – Mzk Mar 13 '12 at 05:20

2 Answers2

1

If you do the calculation:

17/2=8.5

ceil(x) will compute the closest integer which is greater than x

round(x) will compute the closest integer. However both 8 and 9 are as close to 8.5. So the convention is that it is equal to 9, in that case.

Similarly round(1.5)=2, round(2.5)=3

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Oli
  • 15,935
  • 7
  • 50
  • 66
  • I can't find any equivalent round(matlab) in C++. Therefore, I'm using ceil. It seems that cvRound(opencv) is not the same as round(matlab). – Mzk Mar 12 '12 at 12:44
  • This answers your question: http://stackoverflow.com/questions/554204/where-is-round-in-c – Oli Mar 12 '12 at 13:02
0

From the answer Where is Round() in C++? , we can answer the question.

However, in Matlab it also has explicit definition. From the Mathworks http://www.mathworks.com/help/techdoc/ref/round.html , we can see

Y = round(X) rounds the elements of X to the nearest integers. Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer. For complex X, the imaginary and real parts are rounded independently.

Of course, we can see

B = ceil(A) rounds the elements of A to the nearest integers greater than or equal to A. For complex A, the imaginary and real parts are rounded independently.

http://www.mathworks.com/help/techdoc/ref/ceil.html

Then we can easily know why round(8.5)=9 and ceil(8.5)=9.

Community
  • 1
  • 1
Neomatrix
  • 31
  • 4