Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]Image.LoadFromFile fails?  (Read 2985 times)

0 Members and 2 Guests are viewing this topic.

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
[Solved]Image.LoadFromFile fails?
« on: May 24, 2013, 08:09:29 pm »
Hello SFML-Community!

I've still done some projects with sfml but with my new project I seem to fail very fast.

I created a class that loads an Image from File and scans the x and y Coordinate where the Image really starts. (White gets ignored)

The LoadFromFile() Function allways tells me it failed. Here is the code I guess its something very stupid:

main.cpp
#include "Word_Position.hpp"

Position Position("captcha.jpg");

int main()
{
    Position.SetWordRect();

    std::cout << "Left: " << Position.tX
                << "\nTop: " << Position.tY
                << "\nWidth: " << Position.tWidth
                << "\nHeight: " << Position.tHeight
                << std::endl;
}
 

Word_Position.cpp
/*
        Word_Position.cpp

        Paul Brandt
        24.05.2013

    This code preforms a simple Pixel-by-Pixel check, to get the position and size of the word in the captcha.
*/


#include "Word_Position.hpp"



bool Position::PixelIsNonWhite(int pX, int pY)  //Preform a simple test if one single pixel is set to a non-white color
{
        if(CaptchaImage.GetPixel(pX, pY) != sf::Color(255, 255, 255))   //Check if the Pixel has an other color than white
                return true;    //Pixel is not white (HIT)
        else
                return false;   //Pixel is white (NO HIT)
}

void Position::SetWordRect() //Store the Position, Size(Pixel) of a word we found. Also provides some usefull memberfunctions.
{

        //Our first step is to find out the X-Coordinate
        for(int x = 0; x<IMAGE_WIDTH; x++)      //Use the X-Coordinate as Outer-LOOP
        {
                for(int y = 0; y<IMAGE_HEIGHT; y++)     //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tX = x;
                }
        }

        //Next step is to find out the Y-Coordinate
        for(int y = 0; y<IMAGE_HEIGHT; y++)     //Use the Y-Coordinate as Outer-LOOP
        {
                for(int x = 0; x<IMAGE_WIDTH; x++)      //Use the Y-Coordinate as Inner-LOOP
        {
                        if(PixelIsNonWhite(x, y)) tY = y;
                }
        }

        // We did some great work on finding the upper left corner
        // Now we will find the downer right corner.
        // Same procedure just the other was round:

        //Our first step is to find out the X-Coordinate
        for(int x = IMAGE_WIDTH; x>0; x--)      //Use the X-Coordinate as Outer-LOOP
        {
                for(int y = IMAGE_HEIGHT; y>0; y--)     //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tWidth = x - tX;
                }
        }

        //Next step is to find out the Y-Coordinate
        for(int y = IMAGE_HEIGHT; y>0; y--)     //Use the Y-Coordinate as Outer-LOOP
        {
                for(int x = IMAGE_WIDTH; x>0; x--)      //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tHeight = y - tY;
                }
        }
}

Position::Position(const char* Filename)
{
    CaptchaImage.Create(IMAGE_WIDTH, IMAGE_HEIGHT, sf::Color(0, 0, 0));    //Create an empty Image
    if(!CaptchaImage.LoadFromFile(Filename));    //Load the file into our Image
    {
        std::cout << "I couldnt read the image from " << Filename << std::endl;
        exit(12);
    }
}
 


Word_Position.hpp:
#include <SFML/Graphics.hpp>    //We use the SFML library to preform our image-checks
#include <iostream>


#define IMAGE_WIDTH     300             //300 by 57 is the Defaut Image Size in ReCaptcha.
#define IMAGE_HEIGHT    57              //Change it with you needs.

#define POS_ENABLED             true    //Define if we want to scan for pixel with tolerance in Position - Doesnt make that much sense without
#define ROT_ENABLED             false   //Define if we want to scan for pixel with tolerance in Rotation - Mostly we dont need this
#define POS_TOLERANCE   0.05f
#define ROT_TOLERANCE   0.10f
#define COLOR_TOLERANCE 0.30f


class Position
{
    private:
        sf::Image CaptchaImage;
        bool PixelIsNonWhite(int pX, int pY);

    public:
        void SetWordRect();
        Position(const char* Filename);

        int tX; //We store the Coordinates public since this is the fastest way to access them.
        int tY;

        int tWidth; //Same here
        int tHeight;



};
 

What am i doing wrong? Is it someting in the Code or am i failing about the Image-Path or Image-Format here?
« Last Edit: May 25, 2013, 04:22:07 pm by Luapina »

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Re: Image.LoadFromFile fails?
« Reply #1 on: May 24, 2013, 09:17:22 pm »
Hi

Can you please try using the minimal example code to load and display an image, and make sure this works before we try and figure out why your code does not work ;)

SFML 2.1

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Image.LoadFromFile fails?
« Reply #2 on: May 24, 2013, 09:29:37 pm »
I JUST had this issue in another thread. Quite literally.

The solution was to load the picture into Paint and save it again.
Or use another picture.

The reason it fails is because the picture most likely is 8-bit which is not supported. It needs to be at least 24 or 32.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Image.LoadFromFile fails?
« Reply #3 on: May 24, 2013, 09:54:21 pm »
Was there no specific error message on the standard output, such as "file format not supported"?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Image.LoadFromFile fails?
« Reply #4 on: May 25, 2013, 11:14:38 am »
The function itself did NOT give me any error Message. I just know it fails beacause of my own error check:

    if(!CaptchaImage.LoadFromFile(Filename));    //Load the file into our Image
    {
        std::cout << "I couldnt read the image from " << Filename << std::endl;
        exit(12);
    }

Im also sure that it is NOT a problem with incompatibility because i also tried loading an image i loaded in earlier projects.

Tried the minimal example code in another project and it worked. Ill try it in this project as well soon.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Image.LoadFromFile fails?
« Reply #5 on: May 25, 2013, 12:03:16 pm »
I'm not sure how it's handled in SFML 1.6, but in SFML 2.0 afaik the load function can't fail without giving a certain error message.

Besides you should really go with SFML 2. 1.6 is depreciated, unmaitained and buggy. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Image.LoadFromFile fails?
« Reply #6 on: May 25, 2013, 04:20:39 pm »
OK. So this kind of solved now. Ill switch to 2.0.
The only reason why I used 1.6 is, that it is still the newest Version of sfml in most linux repositories.

Thanks,
Luapina