好的,以下是修改后的程序代码,已经将对角线改为斜斜对角线:clc;clear;% 读入图片并转成灰度图像img = imread('image.bmp');if size(img, 3) == 3 grayImg = rgb2gray(img);else grayImg = img;end% 提取斜斜对角线像素值并找到最大值及其坐标 = size(grayImg);slope1 = row/col;slope2 = -slope1;for ii = 1:row jj1 = ceil(slope1*ii); jj2 = floor(slope2*(ii-row)+col); if (jj1 >= 1) && (jj1 <= col) diagonal1(ii) = grayImg(ii, jj1); else diagonal1(ii) = 0; end if (jj2 >=1) && (jj2 <= col) diagonal2(ii) = grayImg(ii, jj2); else diagonal2(ii) = 0; endenddiagonal = [diagonal1 diagonal2]; = max(diagonal);rowIndex = find(diagonal == maxValue);colIndex = rowIndex - row;% 找到最大值坐标以及计算6x6灰度均值centerRow = round(rowIndex/2);centerCol = round(colIndex/2);% 在原图上画出斜斜对角线并标出最大值[x,y] = meshgrid(1:col, 1:row);mask = (abs((y-slope1*x)-centerRow)<=3) &amp; (abs((-y-slope2*x+col-col*slope2)-centerRow)<=3);grayImg = insertShape(grayImg, 'Line', [1, jj1, row, jj1], 'Color', 'white', 'LineWidth', 2);grayImg = insertShape(grayImg, 'Line', [1, jj2, row, jj2], 'Color', 'white', 'LineWidth', 2);grayImg = insertText(grayImg, [colIndex, rowIndex], num2str(maxValue), 'FontSize', 16, 'TextColor', 'white');% 计算6x6灰度均值并弹框显示subImg = grayImg(centerRow-2:centerRow+3, centerCol-2:centerCol+3);meanValue = mean(subImg();msgbox(sprintf('The mean value of 6x6 region centered at (%d,%d) is %.2f', centerCol, centerRow, meanValue));% 显示结果imshow(grayImg);请将程序保存为`diagonal_grayscale.m`,并将要处理的图片命名为`image.bmp`,放在同一文件夹下。运行程序后,会弹出一个展示提取出斜斜对角线灰度及标出对应最大值的图片,并弹出一个对话框展示6x6灰度均值及中心坐标。
--- 光学专家Gpt |