Originally Posted by
tabstop
The error message seems pretty clear: you cannot initialize a variable-length object. (I.e., initialization is a compile-time activity, and the compiler has no idea what length is going to be in the future.) You'll have to use a loop.
thats tabstop, that makes sense. I have one last question Im stumped on. Im trying to calculate the number of white pixels per row, but so far I can only get it calculate the total number of white pixels in the image. How do I make it calculate the total number of white pixels per row? Here is the code I have so far:
Code:
#include <iostream>
#include <stdio.h>
#include "CImg.h"
#include <vector>
using namespace cimg_library;
//main program
int main()
{
CImg<float> img("xray.jpg");
const unsigned int width = img.dimx();
const unsigned int height = img.dimy();
//converts the image to black and white
img.RGBtoYCbCr().channel(0).resize(-100,-100,1,3).RGBtoLUT(CImg<>(2,1,1,3).fill(0.0f,255.0f),false);
//displays image
CImgDisplay display(img,"Black and White");
while (!display.is_closed)
display.wait();
printf("The height of the image is %d\n",height);
printf("The width of the image is %d\n",width);
//counter array
int counter[height];
int number = 0;
//go through image left to right, top to bottom
for (int i=0;i<width;i++)
{
for (int j=0;j<height;j++)
{
//if pixel equals white, add to number
if (img(i,j,0,0) == 255 && img(i,j,0,1) == 255 && img(i,j,0,2) == 255)
{
number=number + 1;
}
}
}
printf("The number of white pixels is %d\n", number);
int x;
//for each row, print the total number of white pixels for that row
for (x =0; x < height; x++)
{
printf("%d %d\n", x, number);
}
return 0;
}