C#是dotnet的代表虽然不是一个东西但是在这里代表同一件事,不要在意细节。
pythonnet是 python 和.net无缝连接的桥梁。那么python的图像是numpy表示,C#图象是Bitmap。
做图像想要python的便利又想要dotnet的强大就需要图像类型转换。
上程序。
1.Bitmap_转opencv-python
import clr
import numpy as np
import cv2
from System.IO import MemoryStream
clr.AddReference('System.Drawing')
from System.Drawing import Bitmap
# 确保已经加载了System.Drawing程序集
# 假设你已经有了一个Bitmap对象
# 例如,从文件加载一个Bitmap对象
bitmap = Bitmap("8.bmp")
# 将Bitmap转换为字节数组
def bitmap_to_bytes(bitmap):
stream = MemoryStream()
bitmap.Save(stream, bitmap.RawFormat) # 保存图像到流中
stream.Position = 0 # 重置流的位置
return np.frombuffer(stream.ToArray(), dtype=np.uint8)
bitmap_data = bitmap_to_bytes(bitmap)
# 使用OpenCV的imdecode函数将字节数组解码为Mat对象
mat = cv2.imdecode(bitmap_data, cv2.IMREAD_COLOR)
# 现在你可以使用OpenCV的功能处理这个Mat对象了
# 例如,将其转换为灰度图像
gray_mat = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY)
# 显示图像
cv2.imshow("Gray Image", gray_mat)
cv2.imshow("GrImage", mat)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.numpy转bitmap
import clr
import numpy as np
import cv2
from System.IO import MemoryStream
clr.AddReference('System.Drawing')
from System.Drawing import Bitmap, Imaging
# 读取图像(确保路径正确)
image_path = "Lena.png"
cv_image = cv2.imread(image_path)
# 将BGR格式转换为RGB格式
cv_image_rgb = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
# 创建一个与图像数据相匹配的numpy数组
h, w, c = cv_image_rgb.shape
numpy_array = np.array(cv_image_rgb, dtype=np.uint8).reshape((h, w, c))
# 创建一个MemoryStream对象并将numpy数组写入
stream = MemoryStream()
cv2.imencode('.png', numpy_array)[1].tobytes()
stream.Write(cv2.imencode('.png', numpy_array)[1].tobytes(), 0, len(cv2.imencode('.png', numpy_array)[1].tobytes()))
stream.Position = 0
# 使用.NET的System.Drawing命名空间中的Bitmap类从MemoryStream创建Bitmap对象
bitmap = Bitmap.FromStream(stream)
# 现在你有一个System.Drawing.Bitmap对象,可以在.NET环境中使用
# 例如,保存到文件
bitmap.Save("output_image.png", Imaging.ImageFormat.Png)
# 清理资源
stream.Close()
3.互相转换:
import clr
import numpy as np
import cv2
from System.IO import MemoryStream
clr.AddReference('System.Drawing')
from System.Drawing import Bitmap, Imaging
bitmap = Bitmap("lena.jpg")# 读取 System.Drawing.Bitmap对象
def bitmap_to_mat(bitmap,flag=False):
r"""
System.Drawing.Bitmap对象
转换为
opencv 图像
"""
stream = MemoryStream()
bitmap.Save(stream, bitmap.RawFormat)
stream.Position = 0
bitmap_data = np.frombuffer(stream.ToArray(), dtype=np.uint8)
stream.Close() #一定要
mat = cv2.imdecode(bitmap_data, cv2.IMREAD_COLOR)
if flag :
gray_mat = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY)
return gray_mat
bgr_mat = matimdecode #已经是BGR 所以 不需要#bgr_mat = cv2.cvtColor(mat, cv2.COLOR_RGB2BGR)
return bgr_mat
bgr_mat = bitmap_to_mat(bitmap)# 0opencv 的BGR图
gray_mat = bitmap_to_mat(bitmap,True)#opencv 的 灰度图
# 显示图像
cv2.imshow("bgr_mat", bgr_mat)
cv2.imshow("gray_mat", gray_mat)
# 读取图像(确保路径正确)
image_path = "lena.jpg"
cv_image = cv2.imread(image_path)
def mat_to_bitmap(cv_image):
r"""
opencv 图像
转换为
System.Drawing.Bitmap对象
"""
cv_image_rgb = cv_image#imencode imdecode 已经是BGR 所以 不需要cv2.COLOR_BGR2RGB
h, w, c = cv_image_rgb.shape# 创建一个与图像数据相匹配的numpy数组
numpy_array = np.array(cv_image_rgb, dtype=np.uint8).reshape((h, w, c))
stream = MemoryStream()# 创建一个MemoryStream对象并将numpy数组写入
cv2.imencode('.png', numpy_array)[1].tobytes()
stream.Write(cv2.imencode('.png', numpy_array)[1].tobytes(), 0, len(cv2.imencode('.png', numpy_array)[1].tobytes()))
stream.Position = 0
bitmap = Bitmap.FromStream(stream)# 使用.NET的System.Drawing命名空间中的Bitmap类从MemoryStream创建Bitmap对象
stream.Close()# 清理资源
return bitmap # 现在有一个System.Drawing.Bitmap对象,可以在.NET环境中使用
bitmap = mat_to_bitmap(cv_image)#转换
bitmap.Save("output_image.bmp", Imaging.ImageFormat.Bmp)
cv2.waitKey(0)
cv2.destroyAllWindows()