for (int i = 0; i < bytes; i++)//获取像素点的灰度值,并赋值到tempA[]中
{
curColor = bmGetPixel((i+1) / curBitmapWidth, (i+1) % curBitmapWidth);
ret = (byte)(curColorR 0299 + curColorG 0587 + curColorB 0114);
grayValues[i] = ret;
tempA[i] = ConvertToDouble(grayValues[i]);
}
图像的像素,是从坐标 1,1开始的。
'利用API-GetPixel 获取窗体或某一点的颜色值(long类型)。注意SaleMode属性要设为3
'然后将long换算成rgb值,再判断rgb是否相等(相等的为灰色)
'重复上面几步,进行采样判断。当然如果你确定某一点 不是灰色 就是彩色的话 ,判断 一次就行
’--这样做不是非常精确,但一般还是可以的
不知道楼主是否还要哦 我也有这个需求,但是没找到感觉合适的软件,于是自己写了一段代码,喜欢的话可以用的。
# -- coding:utf-8 --from PIL import Image
from PIL import ImageFile
import sys
from webUrlGetorsettings import
ImageFileLOAD_TRUNCATED_IMAGES = True
class SavePic(object):
def __init__(self):
pass
def getGray(self, image_file):
tmpls = []
for h in range(0, image_filesize[1]): # h
for w in range(0, image_filesize[0]): # w
tmplsappend(image_filegetpixel((w, h)))
return tmpls
def getAvg(self, ls): # 获取平均灰度值
return sum(ls) / len(ls)
def getImgHash(self, fne):
image_file = Imageopen(fne) # 打开
image_file = image_fileresize((480, 480)) # 重置大小我12px X 12px
image_file = image_fileconvert("L") # 转256灰度图
Grayls = selfgetGray(image_file) # 灰度集合
avg = selfgetAvg(Grayls) # 灰度平均值
bitls = '' # 接收获取0或1
# 除去变宽1px遍历像素
for h in range(1, image_filesize[1] - 1): # h
for w in range(1, image_filesize[0] - 1): # w
if image_filegetpixel((w, h)) >= avg: # 像素的值比较平均值 大于记为1 小于记为0
bitls = bitls + '1'
else:
bitls = bitls + '0'
return bitls
def getMH(self, a, b): # 比较100个字符有几个字符相同
dist = 0
print a
print b
for i in range(0, len(a)):
if a[i] == b[i]:
dist = dist + 1
dist_done = format(float(dist) / float(len(a)), accuracy)
return dist_done
def compare_pic_hash(self, hash_a, hash_b):
"""
:param hash_a: A的hash
:param hash_b: b的hash
:return:
"""
compare = selfgetMH(hash_a, hash_b)
return compare
def get_file_name_list_from_path(self, file_path):
"""
:return:
"""
file_name_list = []
for file_name in oslistdir(file_path):
if ospathgetsize(ospathjoin(file_path, file_name)) < 102400:
try:
osremove(ospathjoin(ospathjoin(file_path, file_name)))
except Exception as e:
print "删除失败,原因:", str(e)
else:
if not (ospathsplit(file_name)[1]endswith("jpg") or ospathsplit(file_name)[1]endswith("png")):
pass
else:
file_name_listappend(file_name)
return file_name_list
def get_file_hash_list_from_file_name_list(self, file_path, file_name_list):
"""
:return:
"""
file_hash_list = []
try:
for i in range(0, len(file_name_list)):
hash_of_file = selfgetImgHash(ospathjoin(file_path, file_name_list[i]))
file_hash_listappend(hash_of_file)
selfview_bar(i, len(file_name_list))
except Exception as e:
print "获取文件hash错误!", str(e)
return file_hash_list
def getDocSize(self, path):
try:
size = ospathgetsize(path)
return size
except Exception as err:
print(err)
def view_bar(self, num, total):
rate = float(num) / total
rate_num = int(rate 100) + 1
r = '\r[%s%s]%d%%' % ("#" rate_num, " " (100 - rate_num), rate_num,)
sysstdoutwrite(r)
sysstdoutflush()
def compare_pic_hash_list(self, file_hash_list, file_path, file_name_list):
"""
:param file_name_list:
:param file_path:
:param file_hash_list:
:return:
"""
if len(file_name_list) != len(file_hash_list):
sysexit("hash list 与 file list下标不一致!")
total = (len(file_name_list) (len(file_name_list) - 1)) / 2
print "\n共需要对比", total
need_delete_list_file_name = []
count = 0
for i in range(0, len(file_hash_list)):
for j in range(i + 1, len(file_hash_list)):
compare = selfcompare_pic_hash(file_hash_list[i], file_hash_list[j])
if float(compare) - float(like) > 0:
file_i_size = selfgetDocSize(ospathjoin(file_path, file_name_list[i]))
file_j_size = selfgetDocSize(ospathjoin(file_path, file_name_list[j]))
if file_i_size - file_j_size > 0:
need_delete_list_file_nameappend(file_name_list[j])
break
else:
need_delete_list_file_nameappend(file_name_list[i])
continue
count = count + 1
selfview_bar(count, total)
news_ids = []
for need_delete_file_path in need_delete_list_file_name:
if need_delete_file_path not in news_ids:
news_idsappend(need_delete_file_path)
print "\n去重后的需要删除的长度", len(news_ids)
return news_ids
def delete_file(self, file_path, need_delete_list_file_name):
"""
:return:
"""
for need_delete_file_name in need_delete_list_file_name:
try:
osremove(ospathjoin(file_path, need_delete_file_name))
except Exception as e:
print "删除失败,原因:", str(e)
def main(self, file_path):
"""
aa
:return:
"""
# 获取所有文件的文件名,放入list
# 获取所有文件的hash 多线程
# 对比文件hash值
# 如果hash值相似度大于95%
# 删小留大,否则pass 将需要删除的文件放入 待删除list
# 单线程删除文件
file_name_list = selfget_file_name_list_from_path(file_path)
print "文件夹下共包含文件", len(file_name_list), "个"
file_hash_list = selfget_file_hash_list_from_file_name_list(file_path, file_name_list)
need_delete_list_file_name = selfcompare_pic_hash_list(file_hash_list, file_path, file_name_list)
need_delete_list_file_name_new = []
for item in need_delete_list_file_name:
if "uncensored" not in item:
need_delete_list_file_name_newappend(item)
selfdelete_file(file_path, need_delete_list_file_name_new)
return need_delete_list_file_name_new
def file_name(self, file_dir):
for root, dirs, files in oswalk(file_dir):
return dirs # 当前路径下所有子目录
if __name__ == '__main__':
x = SavePic()
now_dir = ospathdirname(ospathabspath(__file__))
project_dir = ospathsplit(now_dir)[0]
img_path = ospathjoin(project_dir, "images")
img_info = xfile_name(img_path)
# print img_info
# for i in range(0, len(img_info)):
# print "当前对比的文件夹", img_info[i]
# for j in range(0, 3):
# print "正在进行的去重次数", i
# file_patha = ospathjoin(img_path, img_info[i])
# need_delete_list_file_name = xmain(file_patha)
# if len(need_delete_list_file_name) == 0:
# break
for j in range(0, 3):
print "正在进行的去重次数", j
# file_patha = ospathjoin(img_path, tags)
file_patha = ospathjoin(img_path, "kaetzchen")
need_delete_list_file_name = xmain(file_patha)
if len(need_delete_list_file_name) == 0:
break
ossystem('say "去重完成!"')
用point 方法太慢了,还是用api的getDIBits和setDIBits吧,彩图转灰度图的代码如下:
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
Private Type BitMapInfoHeader ''文件信息头——BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
''rgbReserved As Byte
End Type
Private Type BitMapInfo
bmiHeader As BitMapInfoHeader
bmiColors As RGBQuad
End Type
Private Sub Command1_Click()
Dim ix As Integer
Dim iy As Integer
Dim iWidth As Integer '以像素为单位的图形宽度
Dim iHeight As Integer '以像素为单位的图形高度
Dim bytGray As Byte
Dim bytThreshold As Byte
Dim bits() As Byte '三维数组,用于获取原彩色图像中各像素的RGB数值以及存放转化后的灰度值
Dim bitsBW() As Byte '三维数组,用于存放转化为黑白图后各像素的值
'获取图形的宽度和高度
iWidth = Picture1ScaleWidth / ScreenTwipsPerPixelX
iHeight = Picture1ScaleHeight / ScreenTwipsPerPixelY
Picture1Picture = Picture1Image
'创建并初始化一个bitMapInfo自定义类型
Dim bi24BitInfo As BitMapInfo
With bi24BitInfobmiHeader
biBitCount = 32
biCompression = 0&
biPlanes = 1
biSize = Len(bi24BitInfobmiHeader)
biWidth = iWidth
biHeight = Picture1ScaleHeight / ScreenTwipsPerPixelY
End With
'重新定义数组大小
ReDim bits(3, 0 To iWidth, 0 To iHeight) As Byte
ReDim bitsBW(3, 0 To iWidth, 0 To iHeight) As Byte
'使用GetDIBits方法一次性获取picture1中各点的rgb值,比point方法或getPixel函数逐像素获取像素rgb要快出一个数量级
lrtn = GetDIBits(Picture1hdc, Picture1PictureHandle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&)
'数组的三个维度分别代表像素的RGB分量、以图形左下角为原点的X和Y坐标。
'具体说来,这时bits(0,2,3)代表从图形左下角数起横向第2个纵向第3个像素的Blue值,而bits(1,2,3)和bits(2,2,3)分别的Green值和Red值
bytThreshold = 128 '这里定义转换为黑白图像时的阈值为128,即灰色亮度大于128的像素转为白色,小于128的像素转为黑的,此值可根据需要修改为0-255之前任意数值
For ix = 0 To iWidth
For iy = 0 To iHeight
'RGB转为灰度的算法有多种,这里给出常见的两种
'bytGray = bits(0, ix, iy) 011 + bits(1, ix, iy) 059 + bits(2, ix, iy) 03 '这是传统的根据三原色亮度加权得到灰阶的算法
bytGray = (bits(0, ix, iy) ^ 22 00722 + bits(1, ix, iy) ^ 22 07152 + bits(2, ix, iy) ^ 22 02126) ^ (1 / 22) '这是简化 sRGB IEC61966-21 [gamma=220],有点类似于photoshop中所用的算法
bits(0, ix, iy) = bytGray
bits(1, ix, iy) = bytGray
bits(2, ix, iy) = bytGray
'转为黑白图像
If bits(0, ix, iy) < bytThreshold Then
bitsBW(0, ix, iy) = 0
bitsBW(1, ix, iy) = 0
bitsBW(2, ix, iy) = 0
Else
bitsBW(0, ix, iy) = 255
bitsBW(1, ix, iy) = 255
bitsBW(2, ix, iy) = 255
End If
Next
Next
'将灰度图显示到picture2中
Picture2Picture = Picture2Image '如果picture2的picture属性为空,需要在setDIBits之前将其picture属性设置一下,否则无法显示出图形
SetDIBits Picture2hdc, Picture2PictureHandle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&
Picture2Picture = Picture2Image
'将黑白图显示到picture3中
Picture3Picture = Picture3Image
SetDIBits Picture3hdc, Picture3PictureHandle, 0&, iHeight, bitsBW(0, 0, 0), bi24BitInfo, 0&
Picture3Picture = Picture3Image
End Sub
代码运行很快,如图所示基本可瞬间完成
以上就是关于c# 图像去噪 错误提示 参数必须为正且小于高度 如何解决全部的内容,包括:c# 图像去噪 错误提示 参数必须为正且小于高度 如何解决、VB 如何识别图像时RGB颜色模式,还是灰度颜色模式、每次看到喜欢的图就存,导至图片有很多存重复的,一直想把重复的图片删掉等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)