Saving image of maze - c++

I created a maze generator using sfml and cpp. Once the program is run it creates a maze saves its specs in the format of txt file and then proceed to save a image. Everything works fine, but if i increase the size of the grid or decrease the size of cell dimensions some part of the maze is not saved in the image.
What i see on the screen [dont worry about the not responding part]
vs what is saved
If you want to see the source code you can find it on my github:Github Link
And if you want a walkthrough of the working of the code, you can read it on my blog:Blog link
Note for Mode: My blog is not monetized as of the timing and i am just treating it as a fun archive for projects and challenges.
if(once)
{
cout<<"maze done!!";
ofstream output("maze.txt");
output<<rows<<" "<<cols<<" "<<cellDimensions<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
for(int k=0;k<4;k++)
{
output<<grid[i][j].wall[k]<<" ";
}
output<<endl;
}
}
// std::chrono::milliseconds timespan(60000);
// std::this_thread::sleep_for(timespan);
sf::Vector2u windowSize = window.getSize();
sf::Texture texture;
texture.create(windowSize.x, windowSize.y);
texture.update(window);
sf::Image screenshot = texture.copyToImage();
screenshot.saveToFile("sc.png");
once=0;
}
I feel like the last few statements where i try to save the image are the problem.
Thanks You!

Related

filling an area of a matrix with characters

I have created a matrix using 2D vectors. The code I used is
int RC=50;
vector<vector<int> > matrix;
vector<int>row;
///////////Building Grid//////////////////
for(int i=0;i<RC;i++)
{
for(int j=0;j<RC;j++)
{
row.push_back(0);
}
matrix.push_back(row);
}
//////////Printing Grid///////////////////
for(int i=0;i<RC;i++)
{
for(int j=0;j<RC;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
The output of the above code is
Now what I want is to fill a block of size 6x6 inside the matrix with '$' or any character by inputting the bottom left location of the block. For example if i gave the location as (10,4), then I would like to place a block of '$' (size 6x6) whose bottom left co-ordinates are (10,4).
EDIT-1
I added the code
int si=3;
int sy=3;
for(int i=0;i<RC;i++)
{
for(int j=0;j<RC;j++)
{
if(i>=si && i<=si+6 && j>=sy && j<=sy+6)
{
matrix[i][j]=1;
}
else
{
matrix[i][j]=0;
}
}
}
and I got the output as
I am reading the co-ordinates as the top left ones, what should I do to read the co-ordinates as the bottom left ones and build the block from there?
You need to tackle the problem logically and break down the steps you need to solve it. You're staring at a big block of zeros and that isn't going to help. So, walk through it with pseudo code and a handy, dandy piece of paper and pencil.
Ask smaller questions about the larger problem at hand. How do you go from bottom left coordinate to the coordinate you wish to start with? How do you determine when to stop? Do I understand what I just did? If not why don't I understand it?
Baby step by baby step while you're learning. Take the time to understand why something either worked the way you wanted it to or failed to work. Do this and you'll be a much better coder for it.

Can't display a PNG using Glut or OpenGL

Code is here:
void readOIIOImage( const char* fname, float* img)
{
int xres, yres;
ImageInput *in = ImageInput::create (fname);
if (! in) {return;}
ImageSpec spec;
in->open (fname, spec);
xres = spec.width;
yres = spec.height;
iwidth = spec.width;
iheight = spec.height;
channels = spec.nchannels;
cout << "\n";
pixels = new float[xres*yres*channels];
in->read_image (TypeDesc::FLOAT, pixels);
long index = 0;
for( int j=0;j<yres;j++)
{
for( int i=0;i<xres;i++ )
{
for( int c=0;c<channels;c++ )
{
img[ (i + xres*(yres - j - 1))*channels + c ] = pixels[index++];
}
}
}
in->close ();
delete in;
}
Currently, my code produces JPG files fine. It has the ability to read the file's information, and display it fine. However, when I try reading in a PNG file, it doesn't display correctly at all. Usually, it kind of displays the same distorted version of the image in three separate columns on the display. It's very strange. Any idea why this is happening with the given code?
Additionally, the JPG files all have 3 channels. The PNG has 2.
fname is simply a filename, and img is `new float[3*size];
Any help would be great. Thanks.`
Usually, it kind of displays the same distorted version of the image in three separate columns on the display. It's very strange. Any idea why this is happening with the given code?
This reads a lot like the output you get from the decoder is in row-planar format. Planar means, that you get individual rows one for every channel one-after another. The distortion and the discrepancy between number of channels in PNG and apparent count of channels are likely due to alignment mismatch. Now you didn't specify which image decoder library you're using exactly, so I can't look up information in how it communicates the layout of the pixel buffer. I suppose you can read the necessary information from ImageSpec.
Anyway, you'll have to rearrange your pixel buffer rearrangement loop indexing a bit so that consecutive row-planes are interleaved into channel-tuples.
Of course you could as well use a ready to use imagefile-to-OpenGL reader library. DevIL is thrown around a lot, but it's not very well maintained. SOIL seems to be a popular choice these days.

Using FLTK & C++: Making a function that fits an image within a defined frame

I am writing a program in which the user can type the name of an image, for example "image.jpg". I am trying to devise a function that gets that image, and without cropping it, resizes it in a manner that allows it to fit in a Rectangle shape of 470 x 410 pixels.
Would anyone happen to know how to obtain the numerical values of the size of the image and/or resize the image so that it fits inside that Rectangle?
There's an example program in the FLTK documentation called pixmap_browser.cpp. On my Linux system, I found it under /usr/share/doc/fltk-1.3.2/examples
Here's the essence of the code you're looking for:
#include <FL/Fl_Shared_Image.H>
// ...
// Load the image file:
Fl_Shared_Image *img = Fl_Shared_Image::get(filename);
// Or die:
if (!img) {
return;
}
// Resize the image if it's too big, by replacing it with a resized copy:
if (img->w() > box->w() || img->h() > box->h()) {
Fl_Image *temp;
if (img->w() > img->h()) {
temp = img->copy(box->w(), box->h() * img->h() / img->w());
} else {
temp = img->copy(box->w() * img->w() / img->h(), box->h());
}
img->release();
img = (Fl_Shared_Image *) temp;
}
The method that does the resizing is Fl_Image::copy. Basically, the code replaces the source image with a resized copy if it's too big.

HOG Descriptor Vector layout and how to save them to text file

I am new to open CV so currently struggling with it. I have extracted HOG features using following definition:
HOGDescriptor hog(Size(16,16), Size(16,16), Size(16,16), Size(8,8), 9);
It returns 36 dimensional feature vector / pixel. Now I want to separate all these 36 values in a row and save it in text file. I don't know how to do it. Please do guide me.
Thanks in advance.
After you compute the features, i.e. descriptors by cv::HOGDescriptor::compute, it's a vector<float>, so just access it like normal vector<float>s.
And if you want to split them into 36-by-36 style, you can do like this:
for (int i=0; i<descriptors.size()/36; i++)
{
// ... handle 36 values here
for (int j=0; j<36; j++)
{
if (36*i+j < descriptors.size()) // make sure not out-of-bound
{
float temp = descriptors[36*i+j];
...
}
}
}

Simple-iphone-image-processing source code question, what does this do

I am going through the source code for the above project and I don't understand the following lines of code can anyone help explain it to me please? I am trying to get the code to work with color images as it currently only works with greyscale images. I have the main methods working however the filters only get applied to the top quarter of the returned images.
//In the heeder file.
inline uint8_t* operator[](const int rowIndex) {
return m_yptrs[rowIndex];
}
//in the .mm file
void Image::initYptrs() {
m_yptrs=(uint8_t **) malloc(sizeof(uint8_t *)*m_height);
for(int i=0; i<m_height; i++) {
m_yptrs[i]=m_imageData+i*m_width;
}
}
From my understanding it looks like it is creating a a reference to the pixels in the images however i don't understand this line of code.
m_yptrs[i]=m_imageData+i*m_width;
Thanks in advance.
Image::initYptrs() initializes an array of pointers to the beginning of each row of the image.
The line in question should probably read
m_yptrs[i] = m_imageData + i*BPP*m_width;
Where BPP is bytes per pixel (e.g. 3 for RGB, 4 for RGBA images).