opencv python C++ 翻转,去噪,灰度反转,灰度压缩,白平衡,对比度,亮度,缩放,角度旋转

import numpy as np
from skimage import data, exposure, img_as_float
import cv2


img = cv2.imread('girl01.jpg')


# 任意角度
# 旋转angle角度,缺失背景白色(255, 255, 255)填充
# def rotate_bound_white_bg(image, angle):
#     # grab the dimensions of the image and then determine the
#     # center
#     (h, w) = image.shape[:2]
#     (cX, cY) = (w // 2, h // 2)
#
#     # grab the rotation matrix (applying the negative of the
#     # angle to rotate clockwise), then grab the sine and cosine
#     # (i.e., the rotation components of the matrix)
#     # -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
#     M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
#     cos = np.abs(M[0, 0])
#     sin = np.abs(M[0, 1])
#
#     # compute the new bounding dimensions of the image
#     nW = int((h * sin) + (w * cos))
#     nH = int((h * cos) + (w * sin))
#
#     # adjust the rotation matrix to take into account translation
#     M[0, 2] += (nW / 2) - cX
#     M[1, 2] += (nH / 2) - cY
#
#     return cv2.warpAffine(image, M, (nW, nH), borderValue=(255, 255, 255))
#
# dst = rotate_bound_white_bg(img, 160)
# resize
# dst = cv2.resize(dst, (640, 480))

# 旋转90 180 270
dst = np.rot90(img, 1)
dst = np.rot90(img, 2)
dst = np.rot90(img, 3)


# 中值滤波/高斯滤波
# dst = cv2.medianBlur(img, 5)
# dst = cv2.GaussianBlur(img,(5,5),0)


# 灰度反转
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img_info = img.shape
# image_height = img_info[0]
# image_weight = img_info[1]
# dst = np.zeros((image_height, image_weight, 1), np.uint8)
# for i in range(image_height):
#     for j in range(image_weight):
#         grayPixel = gray[i][j]
#         dst[i][j] = 255 - grayPixel


# 白平衡
# width = img.shape[1]
# height = img.shape[0]
# dst = np.zeros(img.shape, img.dtype)
#
# # 1.计算三通道灰度平均值
# imgB = img[:, :, 0]
# imgG = img[:, :, 1]
# imgR = img[:, :, 2]
# bAve = cv2.mean(imgB)[0]
# gAve = cv2.mean(imgG)[0]
# rAve = cv2.mean(imgR)[0]
# aveGray = (int)(bAve + gAve + rAve) / 3
#
# # 2计算每个通道的增益系数
# bCoef = aveGray / bAve
# gCoef = aveGray / gAve
# rCoef = aveGray / rAve
#
# # 3使用增益系数
# imgB = np.floor((imgB * bCoef))  # 向下取整
# imgG = np.floor((imgG * gCoef))
# imgR = np.floor((imgR * rCoef))
#
# # 4将数组元素后处理
# maxB = np.max(imgB)
# minB = np.min(imgB)
# maxG = np.max(imgG)
# minG = np.min(imgG)
# maxR = np.max(imgR)
# minR = np.min(imgR)
# for i in range(0, height):
#     for j in range(0, width):
#         imgb = imgB[i, j]
#         imgg = imgG[i, j]
#         imgr = imgR[i, j]
#         if imgb > 255:
#             imgb = 255
#         if imgg > 255:
#             imgg = 255
#         if imgr > 255:
#             imgr = 255
#         dst[i, j] = (imgb, imgg, imgr)


# 亮度调整
# dst= exposure.adjust_gamma(img, 2) #调暗
# dst= exposure.adjust_gamma(img, 0.5) #调亮


# 缩放
# dst=cv2.resize(img,(400,400))


cv2.imshow('111', img)

cv2.imshow("opt", dst)
cv2.waitKey(0)

#include “opencv2/highgui.hpp”
#include <opencv2/core.hpp>
#include
#include <opencv2/opencv.hpp>
#include

int main(int argc, char *argv[])
{
cv::Mat src = cv::imread("…/example/1.png");
cv::Mat dst;

//高斯滤波
// GaussianBlur(src, dst, cv::Size(5, 5), 0, 0);
//中值滤波
// medianBlur(src, dst, 5);

//灰度压缩

//亮度调节   Mat& m, int rtype, double alpha=1(缩放因子,对比度), double beta=0(增量,亮度)
// src.convertTo(dst, src.type(), 1, 1);

//缩放
// cv::resize(src, dst, cv::Size(480, 320), 0, 0, cv::INTER_NEAREST);

//灰度反转
// cv::cvtColor(src, dst, cv::COLOR_BGR2GRAY);
// dst = cv::Scalar(255, 255, 255) - dst;

//翻转
// //>0: 沿y-轴翻转, 0: 沿x-轴翻转, <0: x、y轴同时翻转
// cv::flip(img_Left, image_fliped, -1);

//白平衡
std::vector<cv::Mat> imageRGB;

cv::split(src, imageRGB);
double R, G, B;
B = cv::mean(imageRGB[0])[0];
G = cv::mean(imageRGB[1])[0];
R = cv::mean(imageRGB[2])[0];

double KR, KG, KB;
KB = (R + G + B) / (3 * B);
KG = (R + G + B) / (3 * G);
KR = (R + G + B) / (3 * R);

imageRGB[0] = imageRGB[0] * KB;
imageRGB[1] = imageRGB[1] * KG;
imageRGB[2] = imageRGB[2] * KR;

cv::merge(imageRGB, dst);

cv::imshow("test", dst);
cv::imshow("test1", src);
cv::waitKey(0);
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值