Question

C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is...

C Programming Language

Problem Title: Introduction to Computer Vision

Jojo is an outstanding student who is currently pursuing his Computer Science degree. In one of his Computer Vision lectures, his lecturer tackled a topic about binary

thresholding.

For simplicity, binary thresholding is an image filtering method used to separate low val-
ued pixels (dark) from high valued pixels (bright). Each pixels in an image is classified

as low or high based on a certain threshold K. If a pixel Pij ≤ K, then it is classified as
low pixel, otherwise it is considered as high pixel.


The problem with binary thresholding method is that the value of K may varies from
images. If K is set too a very low value, then a lot of pixels in the image will be considered
high valued pixels, likewise the opposite case. Thus Jojo wants to find the value of K
such that the absolute difference between the amount of dark pixels and bright pixels is
minimum
.

Format Input

The program is expected to read a file ”testdata.txt” which contains at most 500 test cases
of inputs, each containing 2 integers N, M which defines dimension of the image. Each
test case contains N lines of M integers, where each value represents the image pixel
value.

Format Output

Output should be expressed in format ”Case #X: Y ” - X is the number of the test
case, and followed by Y rounded to the nearest integer number, such that when binary
threshold Y is applied to the image, the absolute difference between the amount of dark
pixels and bright pixels in the filtered image is minimum..

Constraints

• 1 ≤ T ≤ 500
• 1 ≤ N , M ≤ 100
• 1 ≤ |S| ≤ 100
• 0 ≤ Pi j ≤ 255

Sample Input (testdata.txt)

2
19 3
1 235 160
2 159 113
162 46 106
108 216 124
22 189 16
60 119 185
156 16 239
76 123 113
83 60 25
52 3 80
184 233 53
168 162 135
6 9 214
124 250 200
195 49 48
8 13 164
91 236 108
229 166 176
58 107 43

7 11
169 127 197 173 160 172 132 202 25 5 79
111 17 106 100 120 231 32 218 43 218 88
238 80 172 106 94 204 102 143 185 160 19
32 1 215 244 178 109 246 110 54 193 89
163 212 229 95 52 7 54 146 61 105 232
44 14 229 54 2 241 22 21 43 44 39
34 235 97 138 200 195 227 106 16 79 167

Sample Output (Standard Output)

Case #1: 113

Case #2: 109

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

 /* * C Program to find optimal value for k in binary threshold */ #include<stdio.h> #include<stdlib.h> // function to sort the array in ascending order void Array_sort(int *array , int n) { // declare some local variables int i=0 , j=0 , temp=0; for(i=0 ; i<n ; i++) { for(j=0 ; j<n-1 ; j++) { if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } // function to calculate the median of the array float Find_median(int array[] , int n) { float median=0; // if number of elements are even if(n%2 == 0) median = (array[(n-1)/2] + array[n/2])/2.0; // if number of elements are odd else median = array[n/2]; return median; } int main(){ // intializing variable int t, n, m, i, k, num_elements, median; // file pointr FILE *fptr; // check if the file is available if ((fptr = fopen("testdata.txt","r")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } // reading no of testcases fscanf(fptr, "%d", &t); // for each testcase for(k=0; k<t; k++){ // the dimension of the array fscanf(fptr, "%d %d", &n, &m); // storing number of elements num_elements = n*m; // initializing the size of array int arr[num_elements]; // storing elements into the array reading from file for(i=0;i<num_elements;i++){ fscanf(fptr, "%d", &arr[i]); } // sorting the elements of the array Array_sort(arr, num_elements); /* median will be the most optimal k value for binary threshold */ // finding median of the array median = Find_median(arr , num_elements); // printing data to stdout printf("CASE #%d: %d\n",k+1, median); } // closing the file pointer fclose(fptr); return 0; }

Explanation: The optimal value for k would be median of the pixels as a median would be int the center of the data it would result in the minimum absolute difference between the bright and dark pixels.

Input:

testdata.txt

2
19 3
1 235 160
2 159 113
162 46 106
108 216 124
22 189 16
60 119 185
156 16 239
76 123 113
83 60 25
52 3 80
184 233 53
168 162 135
6 9 214
124 250 200
195 49 48
8 13 164
91 236 108
229 166 176
58 107 43

7 11
169 127 197 173 160 172 132 202 25 5 79
111 17 106 100 120 231 32 218 43 218 88
238 80 172 106 94 204 102 143 185 160 19
32 1 215 244 178 109 246 110 54 193 89
163 212 229 95 52 7 54 146 61 105 232
44 14 229 54 2 241 22 21 43 44 39
34 235 97 138 200 195 227 106 16 79 167

output:

CASE #1: 113
CASE #2: 109

Add a comment
Know the answer?
Add Answer to:
C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Language Problem Title: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his...

    C Programming Language Problem Title: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his family. Suddenly an instant message came in. Apparently there is a message from Lili : ”Jojo, don’t forget to check the forum. Who knows if there is an assignment before the exam” Jojo finally took the time to check the discussion forum and sure enough, there were some assignments given to his class. He immediately told his classmates. Jojo finds out one difficult problem...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • I have written my code for an employee management system that stores Employee class objects into...

    I have written my code for an employee management system that stores Employee class objects into a vector, I am getting no errors until I try and compile, I am getting the error: C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion). But I am not sure why any help would be great, Thank you! 1 2 3 4 5 6 7 8 9 10 11 12 13...

  • HELP needed urgently....Data and question is attacted below..... Thank you in advance there is no data...

    HELP needed urgently....Data and question is attacted below..... Thank you in advance there is no data link fir this data it’s all in photos.... thank you i have already posted the question We were unable to transcribe this imageNo. 2 3 4 5 6 7 8 Working Sector Public Own Public Public Private Public Private Private Own Own Private Public Public Public Private 9 10 11 12 13 Public IS 16 17 18 19 20 21 22 23 24 25...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT