Octave Atelier

코드와 해설을 함께 읽는 학습 문서

Code Detail

demo-13

이미지를 로드하여 변조하고 그리기

course/basic/demo-13.m

목록으로

코드를 복사해 Octave에서 바로 실행할 수 있습니다.

카테고리

Course Basic

course-basic

코드 길이

76

lines

작성자

won sunggyu

2025-04-22

패키지

none

pkg load

전체 코드

76 lines

# 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 출력
  • 콘솔 텍스트 출력

실행 흐름

  1. 초기화
  2. 데이터 준비
  3. 데이터 연산
  4. RGB to Grayscale 변환
  5. 그래프 그리기
  6. 외곽선 추출 (입력 흑백, 출력은 점으로 표시됨)

핵심 함수

  • ax1
  • imshow
  • set
  • repmat
  • uint8
  • img
  • edge
  • apple_only

실습 과제

  • 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
  • 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
  • 핵심 함수 ax1의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.

같은 카테고리 코드

이전 코드 demo-12c 다음 코드 demo-14