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?