Help with spatial resolution conversion programming (bmp image)

In summary: I][I][I][ EDIT ] Listing edited by mentor for better readability using code tags2. Another possible workaround is to use double quotes instead of single quotes around the array notation. This way, the browser will not interpret it as an index.
  • #1
hilman
17
0
Hello guys. I want to ask something about simple spatial resolution manipulating just by using c language. I have done my programming below, it managed to be compiled but for some reasons the program stucked in the middle when I try to run it. Really hope you guys can help. I am extremely beginner on this.

C:
#include<stdio.h>
#define        width    640
#define        height    581

int main(void)
{   
    FILE *fp;    
    unsignedchar header[54];                    
    unsignedchar img_work[width][height][3];   
    char input_file[128],output_file[128];  
    int v, h, w, i, c, s, ave_w[width], ave_h[height], average_h, average_w;
   

    /*------------Reading image------------*/
    printf("Enter name of the file¥n---");
    scanf("%s",input_file);

    printf("The file that would be processed is %s.¥n", input_file);

    fp=fopen(input_file,"rb");     
    fread(header,1,54,fp); 
    fread(img_work,1,width*height*3,fp); 
    fclose(fp);

    /*------------Spatial Resolution Program------------*/
    printf ("enter level of spatialization-- ");
    scanf ("%d", &v);

    for (i=0; i<v; i++) {
        s = s + s;
    }

    for(c=0; c<3; c++){

        for(h=0; h<height; h++){

            for(w=0; w<width; w=w+s){
                average_w = 0;

                for (i=0; i<s; i++) {
                    ave_w[i] = img_work[w+i][h][c] / s;
                    average_w = average_w + ave_w;
                }

                for (i=0; i<width; i=i+s) {
                    img_work[w+i][h][c] = average_w;
                }
            }
        }
    }

    for(c=0; c<3; c++) {

        for(w=0; w<width; w++) {

            for(h=0; h<height; h=h+s) {
                average_h = 0;

                for (i=0; i<s; i++) {
                    ave_h[i] = img_work[w][h+i][c] / s;
                    average_h = average_h + ave_h[i];
                }

                for (i=0; i<height; i=i+s) {
                    img_work[w][h+i][c] = average_h;
                }
            }
        }
    }

    /*------------Writing File------------*/
    printf("Enter the name of the file that would be saved.¥n---");
    scanf("%s",output_file);

    printf("Name of the file that would be saved is %s.¥n",output_file);

    fp=fopen(output_file,"wb");
    fwrite(header,1,54,fp);
    fwrite(img_work,1,width*height*3,fp); 
    fclose(fp);

    printf("End.¥n");
    return 0;
}
I am really a beginner, so, sorry if it is lacking too much.


[ EDIT ] Listing edited by mentor for better readability using code tags
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
1. Your program is horribly mangled due to the way that the browser renders some array notation that uses i as an index. The browser interprets [i] is the starting tag for italics. One workaround is to use a different index variable, say j, instead of i. Also, some other characters cannot be used -- [b] is the start tag for rendering boldface type, and [s] is the start tag for text that is lined out.

2. When you post your code, please post it with a [ code=c ] tag at the beginning and a [ /code ] tag at the end. (Don't include the extra spaces -- I omitted them so that the browser will show the tags.)
Your code should look something like this:
C:
#include <stdio.h>
#define width 640
#define height 581
.
.
.
int main(void)
{
   // Body of main
}

3. "stucked in the middle" doesn't give us much help, besides which "stucked" is not a word in English. Are there any run-time errors? What is the last line of code that produces output? If you don't know how to use a debugger, a simple alternative is to add printf() statements that show what part of the code is running. Although we can probably figure out why your program isn't working given enough time, most of us won't want to spend the time trying to do so. Help us out by providing as much information as possible about the problem you're having.
 
  • #3
By stuck in the middle you mean its stuck in the for loops, right?

My suggestion is to add print statements inside each loop so you know what index they are on...

as an example in the inner most loop you could print out the value of each index and the value of the loop terminating variables.

In particular the 's' one is suspect and may be very large.

If you have access to a debugger then the print statements won't be needed as the debugger can help you to step through your code and see where things are going wrong. You have to start thinking like a computer to find your bugs.

Lastly, please follow Mark44's advice as it was very painful for me to adjust your listing.
 
  • #4
You are not setting the variable s before you first use it. In C, failing to initialize an automatic variable such as your variable s results in undefined behavior. I suspect that what's happening is that your variable s is initialized to zero. That is not something you should ever depend upon. In this case, that zero initialization is making the loop for(i=0; i<height; i=i+s) {...} loop forever. The increment instruction i=i+s has zero effect because s is zero.
 
  • #5
DH mentioned the killer issue (uninitialized "s", which probably is a negative number and results in an infinite loop). Once that's fixed and the program runs correctly, you'll suffer from poor output quality due to the use of 8-bit numbers in the calculations which leads to various overflows and truncations. There are also initialized but unused arrays (ave_w and ave_h). Remove them and also the " / s" in the s loops so that the code reads "average_w += img_work[w+i][h][c];" then after the loop add "average_w /= s;".

One final comment. Is this an attempt at optimizing a separable image filter? If so, I'd stick with a straightforward implementation so that you can get a known good result and worry about optimization later. You might not even have to worry about optimizing if it's rarely used code (the image is fairly small and the user interacts via the keyboard, so they can't expect zippy performance).
 
  • #6
Sorry for the late reply. I have managed to solve the problem. Sorry for the bad code up here. I've post it when I was really really busy back then...
 

FAQ: Help with spatial resolution conversion programming (bmp image)

How do I convert a BMP image to a higher spatial resolution?

To convert a BMP image to a higher spatial resolution, you will need to use a software program or coding language that supports image processing. This can include programs like Adobe Photoshop or coding languages like Python. The specific steps will vary depending on the program or language you are using, but generally, you will need to resize the image and then use interpolation techniques to fill in the missing pixels and increase the resolution.

What is spatial resolution and why is it important?

Spatial resolution refers to the level of detail or clarity in an image. It is determined by the number of pixels in an image and their size. Higher spatial resolution means more pixels, resulting in a clearer and more detailed image. Spatial resolution is important in image processing as it can affect the accuracy and quality of analysis and visual interpretation.

Is there a maximum limit to how much I can increase the spatial resolution of a BMP image?

Yes, there is a limit to how much you can increase the spatial resolution of a BMP image. This limit is determined by the original image's size and the interpolation techniques used. Increasing the spatial resolution too much can result in a blurry or distorted image due to the interpolation of pixels. It is important to consider the original image's quality and the desired level of detail when increasing the spatial resolution.

How can I decrease the spatial resolution of a BMP image?

To decrease the spatial resolution of a BMP image, you can use a software program or coding language that supports image processing. Similar to increasing the resolution, you will need to resize the image, but this time, you will decrease the number of pixels. This can be done by cropping the image or using resampling techniques to combine multiple pixels into one. It is important to note that decreasing the spatial resolution can result in a loss of detail and clarity in the image.

Are there any other factors besides spatial resolution that can affect the quality of a BMP image?

Yes, there are other factors that can affect the quality of a BMP image. These include color depth, compression, and file format. Color depth refers to the number of colors that can be displayed in an image, while compression can affect the image's overall quality and file size. Additionally, different file formats can also impact the image's quality, with some formats being better for certain types of images. It is important to consider these factors when working with BMP images to ensure the best possible quality.

Similar threads

Replies
2
Views
2K
Replies
4
Views
2K
Replies
75
Views
5K
Replies
22
Views
5K
Replies
5
Views
2K
Replies
10
Views
10K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
2
Views
4K
Back
Top