Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:51:04+00:00 2026-06-01T08:51:04+00:00

Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates

  • 0

Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates through the pixels of an image and outputs either a 0 or 1 for each pixel based on its grayscale value (light or dark). What’s quite strange is that I’m getting different results every time I run the program (with the same input).

If I do

image.display()

it works as expected, so it seems CImg is reading the image properly. However, if I attempt to print AvgVal in the inner for loop, I get different values every time. I’m using OSX 10.7.3 and gcc 4.2.1, if that makes any difference.

#include <iostream>
#include <fstream>
#include <stdexcept>
#include "CImg-1.4.9/CImg.h"
using namespace cimg_library;

int main(int argc, char *argv[]) {
    if (argc != 3) {
        std::cout << "Usage: acepp inputfile outputfile" << std::endl;
    }

    else {
        std::ofstream outputFile(argv[2]);
        if (!outputFile.is_open()) throw std::runtime_error("error: cannot open file for writing");

        CImg<unsigned char> image(argv[1]);
        int RVal, GVal, BVal, AvgVal, outBit;
        for (int iHeight = 0; iHeight < image.height(); iHeight++) {
            for (int iWidth = 0; iWidth < image.width(); iWidth++) {
                RVal = image(iWidth,iHeight,0,0);
                GVal = image(iWidth,iHeight,0,1);
                BVal = image(iWidth,iHeight,0,2);
                AvgVal = (RVal + GVal + BVal) / 3;
                outBit = 1;
                if (AvgVal > 127) outBit = 0; // low is dark, high is light
                outputFile << outBit;
            }
            outputFile << std::endl;
        }
        outputFile.close();
        std::cout << "Done writing to: " << argv[2] << std::endl;
    }

    return 0;
}

I’ve been reading SO for a while, but I just registered, so I can’t post the example images I’m using. I’ll just describe them – they were 10px x 10px png images containing black and white patterns, authored using Photoshop CS5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:51:06+00:00Added an answer on June 1, 2026 at 8:51 am

    If your input .png file is a grayscale one (one channel only, as it is permitted by the .png format), then probably, your Gval and Bval are assigned with random values taken from invalid memory access. As a consequence, your AvgVal may be wrong, and resulting image may be different each time you run the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple MFC program which displays the progressbar..I used the below code
I have written sample program which uses XSLT to generate HTML response. Check below
I have a simple bit of jQuery which displays the row below the current
I have a simple tree which takes the shape below ROOT /\ A B
I have a very simple Winsock2 TCP client - full listing below - which
I have a very simple VB.net Windows Service written using VS.net 2008. The program
i have a simple program which is compiled with gtk2.0 in ubuntu.In ubuntu11.04 i
I'm new to multithreading and try to learn it through a simple program, which
Below I have written a sample program that I have written to learn about
below i have a code that runs in most of my simple programs ..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.