cv-画RGB直方图

python代码

import cv2
from matplotlib import pyplot as plt
import sys


if __name__ == "__main__":
    print("PY file argv is:", sys.argv[1])
    path = sys.argv[1]          # 获取文件名字
    img = cv2.imread(path)      # 读取图片
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)    # 转换成灰度图
    img_gray = img_gray.reshape(-1)  #将图像展开成一个一维的numpy数组
    plt.hist(img_gray, 128)          #将数据分为128组
    firstName = path[:-4]
    secondName = path[-4:]
    name = firstName + '_hist' + secondName
    plt.savefig(name)
    
    histB = cv2.calcHist([img], [0], None, [256], [0, 255])
    histG = cv2.calcHist([img], [1], None, [256], [0, 255])
    histR = cv2.calcHist([img], [2], None, [256], [0, 255])
    plt.figure()
    plt.plot(histB, color='b')
    plt.plot(histG, color='g')
    plt.plot(histR, color='r')
    firstName = path[:-4]
    secondName = path[-4:]
    name = firstName + '_rgb' + secondName
    plt.savefig(name)

shell脚本代码

#!/bin/bash

for file in ./img/*
do
	echo "Now shell is process image:$file"
    python3 hist.py $file
done
  • 运行:sh shell.sh
posted @ 2022-11-03 20:48  starc的miao  阅读(44)  评论(0)    收藏  举报