demo-13
이미지를 로드하여 변조하고 그리기
course/basic/demo-13.m
전체 코드
전체 코드를 복사해서 Octave에서 바로 실행할 수 있습니다.
# filename: demo-13.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 이미지를 로드하여 변조하고 그리기
#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------
run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));
#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------
filename = "fruit.jpg";
img = imread(filename);
[height, width, channel] = size(img); % 340 x 511 x 3
R = img(:, :, 1); % 340 x 511
G = img(:, :, 2); % 340 x 511
B = img(:, :, 3); % 340 x 511
#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------
# RGB to Grayscale 변환
gray_img = rgb2gray(img);
# 사과와 바나나의 색상 마스크 생성
mask_1 = R > 100 & R > G + 80 & R > B + 80; % 340 x 511
mask_2 = G > 100 & G < R - 20 & G > B + 20; % 340 x 511
apple_only = img;
apple_only(repmat(~mask_1, [1,1,3])) = 255;
banana_only = img;
banana_only(repmat(~mask_2, [1,1,3])) = 255;
% 외곽선 추출 (입력 흑백, 출력은 점으로 표시됨)
% "sobel", "canny", "prewitt"
edge_sobel_mask = edge(uint8(mask_1) * 255, "sobel");
edge_sobel_img = uint8(~edge_sobel_mask) * 255;
edge_canny_mask = edge(uint8(mask_1) * 255, "canny");
edge_canny_img = uint8(~edge_canny_mask) * 255;
#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------
figured("Size", [1440, 960], "Move", [-1280, 0], "Name", mfilename);
ax1 = subplots(2, 3);
imshow(img, "Parent", ax1(1, 1));
imshow(repmat(gray_img, [1, 1, 3]), "Parent", ax1(1, 2));
imshow(apple_only, "Parent", ax1(2, 1));
imshow(banana_only, "Parent", ax1(2, 2));
imshow(repmat(edge_sobel_img, [1, 1, 3]), "Parent", ax1(1, 3));
imshow(repmat(edge_canny_img, [1, 1, 3]), "Parent", ax1(2, 3));
set(ax1(1, 1), "Title", "Original Image");
set(ax1(1, 2), "Title", "Grayscale Image");
set(ax1(2, 1), "Title", "Apple Only Image");
set(ax1(2, 2), "Title", "Banana Only Image");
set(ax1(1, 3), "Title", "Sobel Edge Image");
set(ax1(2, 3), "Title", "Canny Edge Image");
set_axes_layout(ax1, [1, 1], [1, 1, 1]); 코드 해설
목적
이미지를 로드하여 변조하고 그리기
입력
- 스크립트 상단에서 정의한 파라미터/입력 데이터를 사용합니다.
출력
- 그래프/figure 출력
- 콘솔 텍스트 출력
실행 흐름
- 초기화
- 데이터 준비
- 데이터 연산
- RGB to Grayscale 변환
- 그래프 그리기
- 외곽선 추출 (입력 흑백, 출력은 점으로 표시됨)
핵심 함수/주제
ax1imshowsetrepmatuint8imgedgeapple_only
실습 과제
- 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
- 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
- 핵심 함수 ax1의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.
학습 팁
같은 카테고리의 다른 코드
- colored
course/basic/colored.m - demo-00
course/basic/demo-00.m - demo-01
course/basic/demo-01.m - demo-02
course/basic/demo-02.m - demo-03a
course/basic/demo-03a.m - demo-03b
course/basic/demo-03b.m - demo-04
course/basic/demo-04.m - demo-05
course/basic/demo-05.m - demo-06
course/basic/demo-06.m - demo-07
course/basic/demo-07.m