hello what I'm currently dealing with is the ability to get input from a text file and then convert it into a bitmap and save it to a file.
the input looks like this:
########
# #
########
and I want to draw it using allegro and instead of # there would be pixels of specified size. Each # should represent a tile (10x10 pixel). So the final result would look like this
link to an image
I've actually drawn it using this code:
for (int i = 0; i < 80; i++){
for (int j = 0; j < 10; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 0; i < 10; i++){
for (int j = 10; j < 20; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 70; i < 80; i++){
for (int j = 10; j < 20; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 0; i < 80; i++){
for (int j = 20; j < 30; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
yeah that's pretty bad, so how do I achieve something like that but with a common procedure which would be independent on the text file? thanks for any advice.
note: the only allowed headers are allegro5/allegro.h and allegro5/allegro_image.h
To draw to an image with Allegro 5, you need to do something like:
ALLEGRO_BITMAP *bmp = al_create_bitmap(640, 480);
al_set_target_bitmap(bmp);
Now all of your drawing operations will happen on the image. To later save it:
al_save_bitmap("somefile.bmp", bmp);
You can also use png and jpg as extensions if your image library has support for it enabled.
Use these functions to read the text file:
al_fopen
al_fgetc
al_feof
al_fclose
Set int x and y to zero. You'll be looping over until the end of the file. On every iteration increment x by one. If you reach a new line character (\n) increment y by one and set x to zero. (You should ignore \r characters.)
Now, depending on the character read, draw a tile:
ALLEGRO_BITAMP *tile_to_draw = NULL;
if (c == '#')
tile_to_draw = bmp1;
else if (c == ' ')
tile_to_draw = bmp2;
if (tile_to_draw)
al_draw_bitmap(tile_to_draw, x * 10, y * 10, 0);
Of course there's better ways to map characters to tiles than a series of ifs, but the above works and should be enough to help you finish your homework.
Related
I am trying to see if I can improve the performance of the following loop in C++, which uses two dimensional vectors (_external and _Table) and has a carried loop dependency on the previous iteration. Additionally, it has a calculated index accessor in the innermost loop that will make the access of _Table non sequential on the right hand side.
int N = 8000;
int M = 400
int P = 100;
for(int i = 1; i <= N; i++){
for(int j = 0; j < M; j++){
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
}
}
What can I do to improve the performance of a loop like this?
Well it looks to me like the order in which these statements:
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
are executed is critical to correctness. (That is, if the iteration order for i, j, k changes, then the results will be different ... and incorrect.)
So I think you are only left with micro-optimizations, like hoisting the expressions _Table.at(j).at(i) and _external.at(j) out of the innermost loop.
Consider this:
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
This loop is repeatedly adding numbers to _Table.at(j).at(i). Since (by inspection) _Table.at(index).at(i-1) must be reading from a different cell of the table (because of i-1 versus i), you could do this:
int temp = 0;
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
temp += _Table.at(index).at(i-1);
}
_Table.at(j).at(i) += temp;
This will reduce the number of calls to at, and may also improve cache performance a bit.
I've got a serial version of BML and I'm trying to write a parallel one with OpenMP. Basically my code works with a main witin a loop calling two functions for horizontal and vertical moves. Like that:
for (s = 0; s < nmovss; s++) {
horizontal_movs(grid, N);
copy_sides(grid, N);
cur = 1-cur;
vertical_movs(grid, N);
copy_sides(grid, N);
cur = 1-cur;
}
Where cur is the current grid. Then horizontal and vertical functions are similar and have a nested loop:
for(i = 1; i <= n; i++) {
for(j = 1; j <= n+1; j++) {
if(grid[cur][i][j-1] == LR && grid[cur][i][j] == EMPTY) {
grid[1-cur][i][j-1] = EMPTY;
grid[1-cur][i][j] = LR;
}
else {
grid[1-cur][i][j] = grid[cur][i][j];
}
}
}
The code produces a ppm image at every step, and whit a certain input the serial version produce an output that we can suppose good. But using #pragma omp parallel for inside the two functions H and V, the ppm file results splitted in such zones as the number of threads(i.e. 4):
I suppose the problem is that every thread should be doing both functions in sequence before termitate because movememnts are strictcly connected. I don't know how to do that. If I set pragma at a highter level like before main loop, there is no speed-up. Obviously the ppm file has to be not sliced like the image.
Goin'on I tried this solution that gives me an identical result as the serial code, but I don't excatly understand why
# pragma omp parallel num_threads(thread_count) default(none) \
shared(grid, n, cur) private(i, j)
for(i = 1; i <= n+1; i++) {
# pragma omp for
for(j = 1; j <= n; j++) {
if(grid[cur][i-1][j] == TB && grid[cur][i][j] == EMPTY) {
grid[1-cur][i-1][j] = EMPTY;
grid[1-cur][i][j] = TB;
}
else {
grid[1-cur][i][j] = grid[cur][i][j];
}
}
}
}
Therefore, if i use just one thread more than available cores(4), the execution time "explodes" instead of remain barely the same.
I am usind pdfbox and trying to colour the header of the table; I had done the table drawing correctly using tutorial ' http://fahdshariff.blogspot.in/2010/10/creating-tables-with-pdfbox.html'
The code as follows
//draw the rows
float nexty = y ;
for (int i = 0; i <= rows; i++) {
contentStream.drawLine(margin, nexty, margin+tableWidth, nexty);
nexty-= rowHeight;
}
//draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
contentStream.drawLine(nextx, y, nextx, y-tableHeight);
nextx += (colWidths != null) ? colWidths[i] : colWidth;
}
I got the code for colouring as follows:
contentStream.setNonStrokingColor( Color.RED );
contentStream.fillRect( 10, 10, 100, 100 );
I tried to put it but its not working as needed. I need to colour table header only. How can I achieve that. Please help me.
i am new in image processing and computer vision and i would like to detect blobs in an image using Laplacian of Gaussian with different scale spaces. The following links explain in detail.
http://www.cs.utah.edu/~jfishbau/advimproc/project1/
http://www.cs.utah.edu/~manasi/coursework/cs7960/p1/project1.html
So far by using opencv2 i have managed to get the images, apply the Gaussian filter with various kernels and apply the Laplacian filter. The i multiply with sigma squared the whole image to amplify the signal (see description in links) and then i apply a threshhold. The next step is to detect local maxima and minima so i can get the blob center and be able to draw circles, but i am not sure how to do it and whether the image processing i have done so far is correct. Here is my code:
int main(){
image1 = imread("butterfly.jpg",0);
drawing1 = imread("butterfly.jpg");
blobDetect(image1,drawing1);
waitKey();
return 0;
}
void blobDetect(Mat image, Mat drawing){
int ksize = 1;
int n =1;
Mat result[10];
for(int i=0; i<10; i++){
cv::GaussianBlur(image,result[i],cv::Size(ksize,ksize),ksize/3,0);
n+=1;
ksize = 2*n-1;
}
ksize = 1;
n =1;
for(int i=0; i<10; i++){
cv::Laplacian(result[i],result[i],CV_8U,ksize,1,0);
n+=1;
ksize = 2*n-1;
}
ksize = 1;
int cols = image.cols;
int rows = image.rows;
for(int a=0; a<10; a++){
for(int i=0; i<rows; i++){
//uchar* data = result[a].ptr<uchar>(rows);
for(int j=0; j<cols; j++){
result[a].at<uchar>(i,j) *= (ksize/3)*(ksize/3);
}
}
ksize++;
ksize = 2*ksize-1;
}
for(int i=0; i<10; i++){
cv::threshold(result[i], result[i], 100, 255, 0);
}
}
This is the expected result
Thanks
After you detect contours, you can use
minEnclosingCircle()
Even better is to check out this tutorial:
http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
I'm writing a Conway's life game for school. In the program I am having trouble with the arrays taking the values I am assigning them. At one point in the program they print out the value assigned to them (1) yet at the end of the program when I need to print the array to show the iterations of the game it shows an incredibly low number. The other trouble was I was encountering difficulties when putting in a loop that would ask if it wants you to run another iteration. So I removed it until the previous errors were fixed.
Im writing this with C++
#include <stdio.h>
int main (void)
{
int currentarray [12][12];
int futurearray [12][12];
char c;
char check = 'y';
int neighbors = 0;
int x = 0; // row
int y = 0; //column
printf("Birth an organism will be born in each empty location that has exactly three neighbors.\n");
printf("Death an organism with four or more organisms as neighbors will die from overcrowding.\n");
printf("An organism with fewer than two neighbors will die from loneliness.\n");
printf("Survival an organism with two or three neighbors will survive to the next generation.\n");
printf( "To create life input x, y coordinates.\n");
while ( check == 'y' )
{
printf("Enter x coordinate.\n");
scanf("%d", &x ); while((c = getchar()) != '\n' && c != EOF);
printf("Enter y coordinate.\n");
scanf("%d", &y ); while((c = getchar()) != '\n' && c != EOF);
currentarray [x][y] = 1;
printf ("%d\n", currentarray[x][y]);
printf( "Do you wish to enter more input? y/n.\n");
scanf("%c", &check); while((c = getchar()) != '\n' && c != EOF);
}
// Note - Need to add a printf statement showing the array before changes are made after input added.
// check for neighbors
while(check == 'y')
{
for(y = 0; y <= 12; y++)
{
for(x = 0; x <= 12; x++)
{
//Begin counting number of neighbors:
if(currentarray[x-1][y-1] == 1) neighbors += 1;
if(currentarray[x-1][y] == 1) neighbors += 1;
if(currentarray[x-1][y+1] == 1) neighbors += 1;
if(currentarray[x][y-1] == 1) neighbors += 1;
if(currentarray[x][y+1] == 1) neighbors += 1;
if(currentarray[x+1][y-1] == 1) neighbors += 1;
if(currentarray[x+1][y] == 1) neighbors += 1;
if(currentarray[x+1][y+1] == 1) neighbors += 1;
//Apply rules to the cell:
if(currentarray[x][y] == 1 && neighbors < 2)
futurearray[x][y] = 0;
else if(currentarray[x][y] == 1 && neighbors > 3)
futurearray[x][y] = 0;
else if(currentarray[x][y] == 1 && (neighbors == 2 || neighbors == 3))
futurearray[x][y] = 1;
else if(currentarray[x][y] == 0 && neighbors == 3)
futurearray[x][y] = 1;
}
}
}
// Set the current array to the future and change the future to 0
{
for(y = 0; y < 12; y++)
{
for(x = 0; x < 12; x++)
{
//Begin the process
currentarray [x][y] = futurearray [x][y];
futurearray [x][y] = 0;
}
}
}
{
for(y = 0; y < 12; y++)
{
for(x = 0; x < 12; x++)
{
//print the current life board
printf("%d ", currentarray[x][y]);
}
}
}
// Have gone through one iteration of Life
//Ask to do another iteration
printf("Do you wish to continue y/n?\n");
scanf("%c", &check); while((c = getchar()) != '\n' && c != EOF);
return 0;
}
You are defining your arrays as [12][12].
In your generation loop you walk from i = 0 to i <= 12, which is 13 steps instead of the 12 of the array. Additionally you are trying to access x-1 and y-1, which can be as low as -1. Again not inside your array.
Sometimes you get semi-useful values from within your array, but on some borders you are just accessing random data.
Try to correct your border.
You forgot to set neighbors to 0 before counting them.
Since this is C++ (not C), you might as well declare neighbors inside the loop body. Makes these kinds of issues easier to spot, too.
Also, is it me, or is that while loop never going to finish? Your braces are a mess, in general, as is your indentation. You could do yourself and us a favour by cleaning those up.
Obviously agree with all the above suggestions. One nice trick you might want to implement with Life is to create an extra border around your area. So if the user wants a 12x12 grid (and you should allow width/height to be specified and allocate memory dynamically) internally you hold a 14x14 grid corresponding to a border around the actual grid. Before running the calculation copy the top row to the bottom border, bottom row to the top border etc. Now you can run the main algorithm on the inner 12x12 grid without worrying about edge cases. This will enable your patterns to re-appear on the other side if they fall off the edge.
You're also forgetting to set the values of both arrays to zero. This will take care of the ridiculous number issue you're having. you can do that by copying this for loop:
for(y = 0; y < 12; y++)
{
for(x = 0; x < 12; x++)
{
//Begin the process
currentarray [x][y] = futurearray [x][y];
futurearray [x][y] = 0;
}
}
and pasting it before the while loop but instead of setting currentarray[x][y] = futurearray[x][y], set it to 0. Also, if the coordinates are viewable locations instead of array co-ordinates, you'll want to change this:
printf ("%d\n", currentarray[x][y]);
to this:
printf ("%d\n", currentarray[x-1][y-1]);
I would also recommend putting a printf with a newline (\n) after each row has been printed and a tab (\t) after each item so that the formatting looks cleaner.