「IT Text 画像工学」という本の画像のポスタリゼーションのコードなのですが、
本によると、4階調のポスタリゼーションカラーに変換されるようです。
しかしソースを読む限り、入力画像と出力画像の結果は同じです。
Pythonに移植して実行してみましたが、結果は変わりません。
なぜでしょうか
https://www.ohmsha.co.jp/book/9784274220074/
cpp
1// 2// sample-2_6.cpp 3// look up table 4// 5// Created by sawano on 2016/09/12. 6// Copyright © 2016年 AIT. All rights reserved. 7// 8#include <iostream> 9#include <opencv2/opencv.hpp> 10 11 12#define VAL_MAX (256) //輝度値の要素数 13#define STEP (4) //ポスタリゼーションの段階数 14//入力画像 15#define FILENAME "input.jpg" 16//ウィンドウ名 17#define WINDOW_INPUT "input" 18#define WINDOW_OUTPUT "output" 19 20int main(int argc, const char * argv[]) { 21 int lut[VAL_MAX]; //ルックアップテーブルの宣言 22 23 //画像の入力 (グレースケール) 24 cv::Mat src_img = cv::imread(FILENAME, 0); 25 if (src_img.empty()) { //入力失敗の場合 26 return (-1); 27 } 28 29 //ルックアップテーブルの生成 30 for (int i=0; i<VAL_MAX; i++) { 31 //ポスタリゼーション 32 lut[i] = (i / (VAL_MAX / STEP)) * (VAL_MAX / STEP); 33 //ネガポジ変換 34 //lut[i] = (VAL_MAX-1) - i; 35 printf("%d: %d\n", i, lut[i]); 36 } 37 38 //出力画像のメモリ確保 39 cv::Mat dst_img = cv::Mat(src_img.size(), CV_8U, 1); 40 41 //画像の走査 (ヒストグラムの生成) 42 for (int y=0; y<src_img.rows; y++) { //縦 43 for (int x=0; x<src_img.cols; x++) { //横 44 dst_img.at<unsigned char>(y, x) = lut[src_img.at<unsigned char>(y, x)]; 45 } 46 } 47 //画像の表示 48 cv::imshow(WINDOW_INPUT, src_img); 49 cv::imshow(WINDOW_OUTPUT, dst_img); 50 cv::waitKey(); //キー入力待ち 51 cv::imwrite("posterization.jpg", dst_img); //画像の保存 52 53 return 0; 54} 55
Python
1# -*- coding: utf-8 -*- 2"""Tone-curve-Postarization.ipynb 3 4Automatically generated by Colaboratory. 5 6Original file is located at 7 https://colab.research.google.com/drive/1jU2JgQ2kJhRBwaTt3S6VyRILmUFueHIc 8""" 9 10!wget http://www.centerportgardenclub.org/wp-content/gallery/photos/2014-marisa-comple-IMG_5867.jpeg img.jpg 11 12mv 2014-marisa-comple-IMG_5867.jpeg img.jpg 13 14ls 15 16# %matplotlib inline 17import cv2 18import matplotlib.pyplot as plt 19 20src_img = cv2.imread("img.jpg", 0); 21plt.imshow( src_img) 22 23max_ = 256 24step_ = 4 25 26lut = [0] * max_ 27for i in range(max_): 28 lut[i] = (i / (max_ / step_)) * (max_ / step_) 29 30dst_img = [] 31for i in range(480): 32 temp = [] 33 for j in range(480): 34 temp.append(0) 35 36 dst_img.append(temp) 37dst_img[1][5] 38 39for y in range(src_img.shape[0]): 40 for x in range(src_img.shape[1]): 41 dst_img[y][x] = lut[src_img[y][x]]; 42 43dst_img[170][239] 44 45src_img.shape 46 47plt.imshow(dst_img) 48 49
回答2件
あなたの回答
tips
プレビュー