Skip to content

Commit 87e4d2b

Browse files
committed
2 parents f1b27f9 + 0cc5587 commit 87e4d2b

File tree

1 file changed

+20
-13
lines changed
  • chapters/image_processing_computer_vision

1 file changed

+20
-13
lines changed

chapters/image_processing_computer_vision/chapter.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ void ofApp::draw(){
5050
ofBackground(255);
5151
ofSetColor(255);
5252

53-
int imgW = myImage.width;
54-
int imgH = myImage.height;
55-
myImage.draw(10, 10, imgW * 10, imgH * 10);
53+
int imgWidth = myImage.width;
54+
int imgHeight = myImage.height;
55+
myImage.draw(10, 10, imgWidth * 10, imgHeight * 10);
5656
}
5757
```
5858

@@ -148,9 +148,14 @@ void ofApp::update(){
148148

149149
// Obtain a pointer to the grabber's image data.
150150
unsigned char* pixelData = myVideoGrabber.getPixels();
151-
152-
// For every byte of the RGB image data,
151+
152+
// Reckon the total number of bytes to examine.
153+
// This is the image's width times its height,
154+
// times 3 -- because each pixel requires 3 bytes
155+
// to store its R, G, and B color components.
153156
int nTotalBytes = camWidth*camHeight*3;
157+
158+
// For every byte of the RGB image data,
154159
for (int i=0; i<nTotalBytes; i++){
155160

156161
// pixelData[i] is the i'th byte of the image;
@@ -227,7 +232,7 @@ It's important to understand how pixel data is stored in computer memory. Each p
227232
228233
![Based on Shiffman's image in the Processing tutorial](images/pixels_in_memory.png)
229234
230-
Observe how the (one-dimensional) list of values have been distributed to successive (two-dimensional) pixel locations in the image — wrapping over the right edge just like English text.
235+
Observe how a one-dimensional list of values in memory can be arranged into successive rows of a two-dimensional grid of pixels, and vice versa.
231236
232237
It frequently happens that you'll need to determine the array-index of a given pixel *(x,y)* in an image that is stored in an `unsigned char*` buffer. This little task comes up often enough that it's worth committing the following pattern to memory:
233238
@@ -236,9 +241,9 @@ It frequently happens that you'll need to determine the array-index of a given p
236241
// unsigned char *buffer, an array storing a one-channel image
237242
// int x, the horizontal coordinate (column) of your query pixel
238243
// int y, the vertical coordinate (row) of your query pixel
239-
// int imgW, the width of your image
244+
// int imgWidth, the width of your image
240245
241-
int arrayIndex = y*imgW + x;
246+
int arrayIndex = y*imgWidth + x;
242247
243248
// Now you can GET values at location (x,y), e.g.:
244249
unsigned char pixelValueAtXY = buffer[arrayIndex];
@@ -252,16 +257,16 @@ Reciprocally, you can also fetch the x and y locations of a pixel corresponding
252257
// Given:
253258
// A one-channel (e.g. grayscale) image
254259
// int arrayIndex, an index in that image's array of pixels
255-
// int imgW, the width of the image
260+
// int imgWidth, the width of the image
256261

257-
int y = arrayIndex / imgW; // NOTE, this is integer division!
258-
int x = arrayIndex % imgW; // The friendly modulus operator.
262+
int y = arrayIndex / imgWidth; // NOTE, this is integer division!
263+
int x = arrayIndex % imgWidth; // The friendly modulus operator.
259264
```
260265

261266
Most of the time, you'll be working with image data that is stored in a higher-level container object, such as an `ofImage`. There are *two* ways to get the values of pixel data stored in such a container. In one method, we can ask the image for its array of unsigned char pixel data, using `.getPixels()`, and then fetch the value we want from this array. Many image containers, such as `ofVideoGrabber`, also support a `.getPixels()` function.
262267

263268
```cpp
264-
int arrayIndex = y*imgW + x;
269+
int arrayIndex = y*imgWidth + x;
265270
unsigned char* myImagePixelBuffer = myImage.getPixels();
266271
unsigned char pixelValueAtXY = myImagePixelBuffer[arrayIndex];
267272
```
@@ -441,7 +446,9 @@ unsigned char redValueAtXY = buffer[rArrayIndex];
441446
unsigned char greenValueAtXY = buffer[gArrayIndex];
442447
unsigned char blueValueAtXY = buffer[bArrayIndex];
443448
```
444-
This is the RGB version of the elementary `index = y*width + x` pattern we used earlier to fetch pixel values from monochrome images.
449+
This is, then, the three-channel "RGB version" of the elementary `index = y*width + x` pattern we used earlier to fetch pixel values from monochrome images.
450+
451+
Note that you may occasionally encounter libraries or hardware which deliver RGB bytes in a different order, such as BGR.
445452

446453
#### Varieties of Image Formats
447454

0 commit comments

Comments
 (0)