山东大学暑期实训第四次记载(第二周第一次)

2024-01-08 13:50

本文主要是介绍山东大学暑期实训第四次记载(第二周第一次),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

      7月5号-6号,根据分配好的任务分工,我进行着查找验证有关图像处理以及骨干分割代码的工作。暂时筛选出5种图像分割的方法:

一.图像分割:

1.Entropy Method图像分割方法

计算前景和背景的信息熵,求最大熵。含义是经过阈值分割后图像的平均信息量越大愈好。

所使用的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
import osdef Entropy(gray):p = []  # 灰度概率H_last = 0  # 上一个H总熵best_k = 0  # 最佳阈值hist = cv2.calcHist([gray], [0], None, [256], [0, 256])  # 255*1的灰度直方图的数组for i in range(256):p.insert(i, hist[i][0] / gray.size)for k in range(256):H_b = 0  # black的熵,前景的平均信息量H_w = 0  # white的熵,背景的平均信息量for i in range(k):if p[i] != 0:H_b = H_b - p[i] * math.log(2, p[i])for i in range(k, 256):if p[i] != 0:H_w = H_w - p[i] * math.log(2, p[i])H = H_b + H_wif H > H_last:H_last = Hbest_k = kreturn H, best_kdef operateImages_path(file_pathname):#遍历处理该目录下的所有图片for filename in os.listdir(file_pathname):print(filename)img = cv2.imread(r""+file_pathname+"/"+filename)  # 读取图像(BGR)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转灰度图像H, best_k = Entropy(gray)ret, thresh1 = cv2.threshold(gray, best_k, 255, cv2.THRESH_BINARY)plt.imsave("D:/AllPycharmProjects/ImageRes/EM"+"/"+filename, thresh1, cmap="gray")  # 保存图片if __name__ == "__main__":operateImages_path("./ImageOps")

图像结果:

2.FCM

所使用的代码:

import cv2
import numpy as np
import os
C = 2
M = 2
EPSILON = 0.001def get_init_fuzzy_mat(pixel_count):global Cfuzzy_mat = np.zeros((C, pixel_count))for col in range(pixel_count):temp_sum = 0randoms = np.random.rand(C - 1, 1)for row in range(C - 1):fuzzy_mat[row, col] = randoms[row, 0] * (1 - temp_sum)temp_sum += fuzzy_mat[row, col]fuzzy_mat[-1, col] = 1 - temp_sumreturn fuzzy_matdef get_centroids(data_array, fuzzy_mat):global Mclass_num, pixel_count = fuzzy_mat.shape[:2]centroids = np.zeros((class_num, 1))for i in range(class_num):fenzi = 0.fenmu = 0.for pixel in range(pixel_count):fenzi += np.power(fuzzy_mat[i, pixel], M) * data_array[0, pixel]fenmu += np.power(fuzzy_mat[i, pixel], M)centroids[i, 0] = fenzi / fenmureturn centroidsdef eculidDistance(vectA, vectB):return np.sqrt(np.sum(np.power(vectA - vectB, 2)))def eculid_distance(pixel_1, pixel_2):return np.power(pixel_1 - pixel_2, 2)def cal_fcm_function(fuzzy_mat, centroids, data_array):global Mclass_num, pixel_count = fuzzy_mat.shape[:2]target_value = 0.0for c in range(class_num):for p in range(pixel_count):target_value += eculid_distance(data_array[0, p], centroids[c, 0]) * np.power(fuzzy_mat[c, p], M)return target_valuedef get_label(fuzzy_mat, data_array):pixel_count = data_array.shape[1]label = np.zeros((1, pixel_count))for i in range(pixel_count):if fuzzy_mat[0, i] > fuzzy_mat[1, i]:label[0, i] = 0else:label[0, i] = 255return labeldef cal_fuzzy_mat(data_array, centroids):global Mpixel_count = data_array.shape[1]class_num = centroids.shape[0]new_fuzzy_mat = np.zeros((class_num, pixel_count))for p in range(pixel_count):for c in range(class_num):temp_sum = 0.Dik = eculid_distance(data_array[0, p], centroids[c, 0])for i in range(class_num):temp_sum += np.power(Dik / (eculid_distance(data_array[0, p], centroids[i, 0])), (1 / (M - 1)))new_fuzzy_mat[c, p] = 1 / temp_sumreturn new_fuzzy_matdef fcm(init_fuzzy_mat, init_centroids, data_array):global EPSILONlast_target_function = cal_fcm_function(init_fuzzy_mat, init_centroids, data_array)print("迭代次数 = 1, 目标函数值 = {}".format(last_target_function))fuzzy_mat = cal_fuzzy_mat(data_array, init_centroids)centroids = get_centroids(data_array, fuzzy_mat)target_function = cal_fcm_function(fuzzy_mat, centroids, data_array)print("迭代次数 = 2, 目标函数值 = {}".format(target_function))count = 3while count < 100:if abs(target_function - last_target_function) <= EPSILON:breakelse:last_target_function = target_functionfuzzy_mat = cal_fuzzy_mat(data_array, centroids)centroids = get_centroids(data_array, fuzzy_mat)target_function = cal_fcm_function(fuzzy_mat, centroids, data_array)print("迭代次数 = {}, 目标函数值 = {}".format(count, target_function))count += 1return fuzzy_mat, centroids, target_functiondef operateImages_path(file_pathname):#遍历处理该目录下的所有图片for filename in os.listdir(file_pathname):print(filename)image = cv2.imread(r""+file_pathname+"/"+filename, cv2.IMREAD_GRAYSCALE)rows, cols = image.shape[:2]pixel_count = rows * colsimage_array = image.reshape(1, pixel_count)# print(image_array[1])# 初始模糊矩阵init_fuzzy_mat = get_init_fuzzy_mat(pixel_count)# 初始聚类中心init_centroids = get_centroids(image_array, init_fuzzy_mat)fuzzy_mat, centroids, target_function = fcm(init_fuzzy_mat, init_centroids, image_array)label = get_label(fuzzy_mat, image_array)new_image = label.reshape(rows, cols)# 黑白反转# r_new_image = 255 - new_image#cv2.imshow("result", new_image)cv2.imwrite("D:/AllPycharmProjects/ImageRes/FCM"+"/"+filename, new_image)  # 保存图像# cv2.waitKey(0)  #表示程序会无限制的等待用户的按键事件# cv2.destroyAllWindows()operateImages_path("./ImageOps")

处理结果:

3.K-means方法

所使用的代码:

import cv2
import math
import numpy as np
import os
import matplotlib.pyplot as pltdef operateImages_path(file_pathname):#遍历处理该目录下的所有图片for filename in os.listdir(file_pathname):print(filename)img = cv2.imread(r""+file_pathname+"/"+filename, cv2.IMREAD_GRAYSCALE)# plt.subplot(221), plt.imshow(img, 'gray'), plt.title('original')# plt.xticks([]), plt.yticks([])# 改变图像的维度img1 = img.reshape((img.shape[0] * img.shape[1], 1))img1 = np.float32(img1)# 设定一个criteria,criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)# 设定一个初始类中心flagsflags = cv2.KMEANS_RANDOM_CENTERS# 应用K-meanscompactness, labels, centers = cv2.kmeans(img1, 2, None, criteria, 5, flags)img2 = labels.reshape((img.shape[0], img.shape[1]))# plt.subplot(222), plt.imshow(img2, 'gray'), plt.title('kmeans_attempts_5')# plt.xticks([]), plt.yticks([])plt.imsave("D:/AllPycharmProjects/ImageRes/KM"+"/"+filename, img2, cmap="gray")  # 保存图片# plt.savefig("kmeans_attempts.png")# plt.show()operateImages_path("./ImageOps")

处理结果:

4.熵算法

所使用的代码:


import cv2
import math
import numpy as np
import osdef caleGrayHist(image):# 灰度图像的高、宽rows, cols = image.shape# 存储灰度直方图grayHist = np.zeros([256], np.uint64)  # 图像的灰度级范围是0~255for r in range(rows):for c in range(cols):grayHist[image[r][c]] += 1return grayHistdef threshEntropy(image):rows, cols = image.shape# 求灰度直方图grayHist = caleGrayHist(image)# 归一化灰度直方图,即概率直方图normGrayHist = grayHist / float(rows * cols)# 第一步:计算累加直方图,也成为零阶累矩阵zeroCumuMoment = np.zeros([256], np.float32)for k in range(256):if k == 0:zeroCumuMoment[k] = normGrayHist[k]else:zeroCumuMoment[k] = zeroCumuMoment[k - 1] + normGrayHist[k]# 第二步:计算各个灰度级的熵entropy = np.zeros([256], np.float32)for k in range(256):if k == 0:if normGrayHist[k] == 0:entropy[k] = 0else:entropy[k] = - normGrayHist[k] * math.log10(normGrayHist[k])else:if normGrayHist[k] == 0:entropy[k] = entropy[k - 1]else:entropy[k] = entropy[k - 1] - normGrayHist[k] * math.log10(normGrayHist[k])# 第三步:找阈值fT = np.zeros([256], np.float32)ft1, ft2 = 0.0, 0.0totalEntropy = entropy[255]for k in range(255):# 找最大值maxFront = np.max(normGrayHist[0:k + 1])maxBack = np.max(normGrayHist[k + 1:256])if maxFront == 0 or zeroCumuMoment[k] == 0 or maxFront == 1 or zeroCumuMoment[k] == 1 or totalEntropy == 0:ft1 = 0else:ft1 = entropy[k] / totalEntropy * (math.log10(zeroCumuMoment[k]) / math.log10(maxFront))if maxBack == 0 or 1 - zeroCumuMoment[k] == 0 or maxBack == 1 or 1 - zeroCumuMoment[k] == 1:ft2 = 0else:if totalEntropy == 0:ft2 = (math.log10(1 - zeroCumuMoment[k]) / math.log10(maxBack))else:ft2 = (1 - entropy[k] / totalEntropy) * (math.log10(1 - zeroCumuMoment[k]) / math.log10(maxBack))fT[k] = ft1 + ft2# 找到最大值索引threshLoc = np.where(fT == np.max(fT))thresh = threshLoc[0][0]# 阈值处理threshold = np.copy(image)threshold[threshold > thresh] = 255threshold[threshold <= thresh] = 0return (thresh, threshold)def operateImages_path(file_pathname):#遍历处理该目录下的所有图片for filename in os.listdir(file_pathname):print(filename)img = cv2.imread(r""+file_pathname+"/"+filename, cv2.IMREAD_GRAYSCALE)the, dst = threshEntropy(img)# the1 = 0# maxval = 255# the1, dst1 = cv2.threshold(img, the1, maxval, cv2.THRESH_TRIANGLE + cv2.THRESH_BINARY_INV)#print('The thresh is :', the)# print('The thresh1 is :', the1)# 黑白反转# r_dst = 255 - dst#cv2.imshow("image", img)#cv2.imshow('shang_thresh_out', dst)# cv2.imshow('thresh_out1', dst1)cv2.imwrite("D:/AllPycharmProjects/ImageRes/SHANG" + "/" + filename, dst)  # 保存图像# cv2.waitKey(0)# cv2.destroyAllWindows()operateImages_path("./ImageOps")

处理结果:

5.直方图法

所使用的代码:

import cv2
import numpy as np
import osdef caleGrayHist(image):# 灰度图像的高、宽rows, cols = image.shape# 存储灰度直方图grayHist = np.zeros([256], np.uint64)  # 图像的灰度级范围是0~255for r in range(rows):for c in range(cols):grayHist[image[r][c]] += 1return grayHistdef threshTwoPeaks(image):# 计算灰度直方图histogram = caleGrayHist(image)# 找到灰度直方图的最大峰值对应得灰度值maxLoc = np.where(histogram == np.max(histogram))firstPeak = maxLoc[0][0]  # 取第一个最大的灰度值# 寻找灰度直方图的第二个峰值对应得灰度值measureDists = np.zeros([256], np.float32)for k in range(256):measureDists[k] = pow(k - firstPeak, 2) * histogram[k]maxLoc2 = np.where(measureDists == np.max(measureDists))secondPeak = maxLoc2[0][0]# 找到两个峰值之间的最小值对应的灰度值,作为阈值thresh = 0if firstPeak > secondPeak:  # 第一个峰值在第二个峰值右侧temp = histogram[int(secondPeak):int(firstPeak)]minLoc = np.where(temp == np.min(temp))thresh = secondPeak + minLoc[0][0] + 1  # 有多个波谷取左侧的波谷else:temp = histogram[int(firstPeak):int(secondPeak)]minLoc = np.where(temp == np.min(temp))thresh = firstPeak + minLoc[0][0] + 1# 找到阈值后进行阈值处理,得到二值图threshImage_out = image.copy()threshImage_out[threshImage_out > thresh] = 255threshImage_out[threshImage_out <= thresh] = 0return (thresh, threshImage_out)# THRESH_TRIANGLE与直方图技术法类似(效果更好)def operateImages_path(file_pathname):#遍历处理该目录下的所有图片for filename in os.listdir(file_pathname):print(filename)img = cv2.imread(r""+file_pathname+"/"+filename, cv2.IMREAD_GRAYSCALE)the, dst = threshTwoPeaks(img)# the1 = 0# maxval = 255# the1, dst1 = cv2.threshold(img, the1, maxval, cv2.THRESH_TRIANGLE + cv2.THRESH_BINARY_INV)#print('The thresh is :', the)# print('The thresh1 is :', the1)# 黑白反转# r_dst = 255 - dst# 进行膨胀和腐蚀#kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))#dst_dilated = cv2.dilate(dst, kernel)#dst_eroded = cv2.erode(dst, kernel)# kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))# dst_dilated = cv2.dilate(dst, kernel1)# kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))# dst_eroded = cv2.erode(dst_dilated, kernel2)#cv2.imshow("image", img)#cv2.imshow('thresh_out', dst)# cv2.imshow('thresh_out_de', dst_eroded)# cv2.imshow('thresh_out_eroded', dst)# cv2.imshow('thresh_out_dilated', dst_dilated)# cv2.imshow('thresh_out1', dst1)cv2.imwrite("D:/AllPycharmProjects/ImageRes/ZHI" + "/" + filename, dst)#cv2.waitKey(0)#cv2.destroyAllWindows()operateImages_path("./ImageOps")

处理结果:

总结:已有的这几个算法对图像的处理效果与我们的预期有些差距,还需要继续改进!

二.骨干提取

       骨架提取,也叫二值图像细化。这种算法能将一个连通区域细化成一个像素的宽度,用于特征提取和目标拓扑表示。morphology子模块提供了两个函数用于骨架提取,分别是Skeletonize()函数和medial_axis()函数。

from skimage import morphology,data,color
import matplotlib.pyplot as plt
import cv2#读入图片
image = cv2.imread('z7777.png',0)
#转为2值图
_,img = cv2.threshold(image,127,255,0)
img[img==255]=1
#实施骨架算法
skeleton =morphology.skeletonize(img)#显示结果
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))ax1.imshow(image, cmap=plt.cm.gray)
ax1.axis('off')
ax1.set_title('original', fontsize=20)ax2.imshow(skeleton, cmap=plt.cm.gray)
ax2.axis('off')
ax2.set_title('skeleton', fontsize=20)fig.tight_layout()
plt.show()

运行结果:

 
 

这篇关于山东大学暑期实训第四次记载(第二周第一次)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/583660

相关文章

暑期学习总结

iOS学习 前言无限轮播图换头像网络请求按钮的configuration属性总结 前言 经过暑期培训,完成了五个项目的仿写,在项目中将零散的内容经过实践学习,有了不少收获,因此来总结一下比较重要的内容。 无限轮播图 这是写项目的第一个难点,在很多项目中都有使用,越写越熟练。 原理为制造两个假页,在首和尾分别制作最后一页和第一页的假页,当移动到假页时,使用取消动画的方式跳到

第四次北漂----挣个独立游戏的素材钱

第四次北漂,在智联招聘上,有个小公司主动和我联系。面试了下,决定入职了,osg/osgearth的。月薪两万一。 大跌眼镜的是,我入职后,第一天的工作内容就是接手他的工作,三天后他就离职了。 我之所以考虑入职,是因为 1,该公司有恒歌科技的freex平台源码,可以学学,对以前不懂的解解惑。 2,挣点素材钱,看看张亮002的视频,他用了6000多,在虚幻商城买的吸血鬼游戏相关的素材,可以玩两年。我

C++中第一次听到构造函数

在C++中第一次听到构造函数这个名词,在C#中又遇到了。   在创建某个类时,由于对该对象的状态(数据)不是很明确,因此需要对其进行初始化。比如说我们要在长方形这个类中创建一个对象,或者说新建一个长方形,那么我们首先要确定他的长和宽,假如我们无法确定它的长和宽,那么我们是无法造出一个长方形来的。所以就要使用这个长方形类中一个用来构造该类所有对象的函数--构造函数。由于该函数要在创建一个新对象

研一实训总结

说长不长说短不短的一个月,从最开始的激动到期间,要中期要兼顾找实习准备笔试面试的焦虑,再到最后一周的加班加点和总结,收获和感触还是蛮多的。 首先,这一个月让我更加全面的认知了完成一个从无到有项目的过程,激发了我对自己工程师职业生涯的向往和对自己有了更广的除了编码以外的要求。 我一直是一个结果导向和追求效率的人,所以在团队合作过程中我们也经历了最开始的不知所措,到争执,再到主动配合和贡献,这个过

我的第一次份实习工作-iOS实习生-第三个月

第三个月 这个月有一个考核项目,是一个电子书阅读器,组长说很重要,是我的实习考核项目。 我的项目XTReader,这是我参考网上的一些代码,和模仿咪咕阅读做的,功能还不完善,数据的部分是用聚合数据做的。要收费的。   还有阅读页面,基本功能实现了一下。使用了autolayout,自适应布局,也是第一次用网络,第一次用数据库,第一次用自动布局。还有很多不足。 做了一周多,有个问题一直没

我的第一次份实习工作-iOS实习生-公司使用过的软件

bittorrentsync 素材,文件同步软件 cornerstone svn 软件开发合作 mark man 测量坐标的软件 SQLLite Manager 数据库操作软件

我的第一次份实习工作-iOS实习生-第二个月

第二个月 来公司过了一个月了。每天早上9点上班,到晚上6.30下班,上下班要指纹打卡,第一个月忘了打卡好多次(),然后还要去补打卡单。公司这边还安排了,工资卡办理,招商银行卡。开了一次新员工大会,认识了公司的一些过往,公司的要求等,还加了一下公司的企业QQ,还有其他的羽毛球群,篮球群。我加了下羽毛球群,也去打了一两次。第二个月的感受,感觉跟组里面的交流跟沟通都好少,基本上还有好多人不认识。想想也

我的第一次份实习工作-iOS实习生-第一个月

实习时间:2015-08-20 到 2015-12-25  实习公司;福建天棣互联有限公司 实习岗位:iOS开发实习生 第一个月: 第一天来公司,前台报道后,人资带我去我工作的地方。到了那,就由一个组长带我,当时还没有我的办公桌,组长在第三排给我找了一个位置,擦了下桌子,把旁边的准备的电脑帮我装了下,因为学的是iOS,实习生就只能用黑苹果了,这是我实习用的电脑。 帮我装了一下电脑后,开机

第一次上传代码到github

1 注册一个github账户 2 最好再设置一个ssh免密设置:http://java-holding.iteye.com/blog/2355969 3 设置好2步骤之后就可以直接不需要输入用户名密码进行上传和下载代码的操作了 4 第一次上传代码到github中的我认为最简单省事的办法:   a、在github上创建一个仓库,如下图所示:               按照步骤新建一个远程仓库之后

剑指offer——第一次只出现一次的字符

/*** */package interview35;/*** 第一次只出现一次的字符* 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置*@author: Administrator*@date: 2017-1-9 下午07:34:07*/import java.util.Scanner;public class Solutio