Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need some help interpreting C code. It is used for Sobel masks (edge detection).

ID: 3890412 • Letter: N

Question

Need some help interpreting C code. It is used for Sobel masks (edge detection). Multi-part question but not long (all parts go with the single program). Any help would be great.

a) In the provided Sobel.c code, the dimensions of the xmask is 3x3, and hence MR is 1 (line 37). Suppose the dimensions of xmask had been 7x7, what would the value of MR need to be?

-I'm thinking MR is either mask size or magnitude? As in 3x3 and MR=1 would mean that a single mask is a 3x3 grid, and that a 7x7 grid would be either mr=9 or mr=5.44..

b) In the double for loop from lines 38-39 and lines 54-55, why does it have mr in the code?

Code: for (i=mr; i<256-mr; i++){

for (j=mr; j<256-mr; j++){

-Obviously I don't know this one since I'm not sure what mr is. but since there's 256-mr a few times it looks like it might have to do with color codes.

c) What is the purpose of maxival, and explain why the code has line 65.

Code: ival[i][j] = (ival[i][j] / maxival) * 255;

-I know color codes go from 0 to 255, so I take it maxival is first set to 0,0,0 (line 53) for black. The maxival then set in lines 57-58 (maxival = ival[i][j];) and it looks like if the color is brighter than black (ival > maxival) then maxival is set to that color. The line 65 code looks like ival and maxival being averaged?

Thanks in advance! I tried but didn't get so far. Currently don't have anything to test the code on either.

#include #include #inc Lude 3 4 5 int pic [256] [256]; int outpicx [256] [256]; 7 6 int outpicy [256] [256]; 8 9 10 lint maskx[3] [3] {{-1,0,1), {-2,0,2}, {-1,0,1}); 11 int masky [3] [3] {{1,2,1), {0,0,0}, {-1,-2,-1)); double ival [256] [256], maxival; 12 int main (int argc, char *argv) int i, j, p, q, mr, sum1, sum2; double threshold; FILE *fo1, *fo2, *fp1, *fopen (); char *foobar; 15 argc-argv+; fp1 fopen (foobar, "rb"); argc-argv+; fol fopen (foobar, "wb"); argc-argv+; threshold = atof(foobar); 19 23 24 25 26 29 30 for (i-0; i

Explanation / Answer

The loops in line 38-39 and 54-55 look for 'mr' number of neighbors row/column surrounding the current pixel and applies maskx and masky on the neighbours to find the correpsonding outputpicx and outputpixy values. So mr indicates the number of neighbor row/col (on each side mr neighbors to be present - up, left, right , bottom) that are considered while calculating the output values. Therefore all rows before 'mr' don't have mr neighbours on top and rows after 256-mr don't have 'mr' rows in bottom and columns before 'mr' don't have mr neighgbors on left and similarly columns after 256-mr don't have mr neighbors on right.

So for mr = 1, you get neighbours that are 1 row up to 1 row below and 1 column before to 1 column after . so

So you get 3x3 grid including the current pixel for a neighbor of length. In the table above x is denoting current cell. Hence for mr = 1 , maskx and masky are of size 3x3.

If you need maskx and masky to be size 7x7, like below

Then it is considering 3 neighbors on each side(left, top, bottom, right). So mr = 3 for a masx of size 7x7

Since we need minimum mr number of rows / column on all siides of the cell, and the size of the 2D array is 256 x 256, the starting value for row/col is mr and ending value is 256-mr (i.e total rows/cols - mr)

The ival values are calculated from outputx and outputy matrix and then the highest of all values in ival is stored in maxival. Now all other values in ival[][] is lesser than this maxival. By dividing each element of ival by maxival ... we get a double value in the range 0 - 1. and this number is then muliplied by 255 since we want to convert it a color value from 0 to 255.

so the line (ival[i][j] / maxival ) * 255 converts maps to one of the color values from 0- 255

Hope the answer helps. If it does , please do rate the answer.

x
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote