掃描以上的標(biāo)準(zhǔn)模板滑動(dòng)采集圖像及其處理
簡(jiǎn) 介:
對(duì)于噴涂有黃色油漆的金屬圓盤進(jìn)行掃描。利用 滑軌帶動(dòng)圓盤 在 V370掃描儀 上移動(dòng),利用300dpi分辨率獲得金屬圓盤模板在掃描儀上不同位置和高度的圖片,通過(guò)后期算法研究掃描儀在不同地方的失真以及影響測(cè)量準(zhǔn)確性的因素。
關(guān)鍵詞:
V370,掃描儀,cv2,HoughCircles
掃描圖片
目 錄
Contents
噴涂黃色油
漆的陰影圈
移動(dòng)距離
掃描結(jié)果
處理圖片
檢測(cè)圓形
檢測(cè)精度問(wèn)題
加速處理
圖片預(yù)分割
Studio帶有GPU環(huán)境
處理總結(jié)
掃描圖片
目 錄
Contents
噴涂黃色油
漆的陰影圈
移動(dòng)距離
掃描結(jié)果
處理圖片
檢測(cè)圓形
檢測(cè)精度問(wèn)題
加速處理
圖片預(yù)分割
Studio帶有GPU環(huán)境
處理總結(jié)
1.1 噴涂黃色油漆的陰影圈
在 獲取棋盤格與標(biāo)準(zhǔn)模板在掃描儀上不同位置圖片 使用金屬陰影圈模板在V370掃描以上進(jìn)行取像的時(shí)候,由于金屬表面的反射使得對(duì)應(yīng)的模板圖像出現(xiàn)亮度變化過(guò)大,會(huì)影響到后期模板圓孔面積的求取,進(jìn)而使得對(duì)掃描儀的掃描精度測(cè)試產(chǎn)生影響。
1.2 移動(dòng)距離
根據(jù) 獲取棋盤格與標(biāo)準(zhǔn)模板在掃描儀上不同位置圖片 測(cè)量蝸桿滑軌的運(yùn)動(dòng)系數(shù):
掃描以上的金屬模板移動(dòng)距離:130cm。根據(jù)上面測(cè)量到的蝸桿滑軌運(yùn)動(dòng)系數(shù),可以知道掃描范圍的步數(shù)為: 130 × 202.53 = 2646 130 \times 202.53 = 2646 130×202.53=2646 。實(shí)際工作中定位2600。
1.2.1 掃描程序
from headm import * from tsmodule.tshardware import * ''' rcccw(2000) exit() ''' epson_title = 'EPSON' save_title = '文件保存設(shè)置' process_title = '進(jìn)程' def epsonScan(): tspsendwindowkey(epson_title, "s", alt=1,noreturn=1) time.sleep(.1) if sum(tspgetwindowrect(save_title)) != 0: tspsendwindowkey(save_title, "\r", noreturn=1) while True: time.sleep(1) if(sum(tspgetwindowrect(process_title)) != 0): break tspbeep(800, 20) while True: time.sleep(1) if(sum(tspgetwindowrect(process_title)) == 0): break tspbeep(1800, 20) printf('\a') for i in range(50): printf("Scan time:%d"%i) epsonScan() rcccw(52) printf('\a')
1.3 掃描結(jié)果
1.3.1 水平移動(dòng)掃描
1.3.2 對(duì)角線移動(dòng)掃描
通過(guò)基于ESP8266 WiFi控制的步進(jìn)升降機(jī)械平臺(tái)移動(dòng)固定步數(shù),測(cè)量移動(dòng)距離。
因此,每移動(dòng)1mm,驅(qū)動(dòng)器需要驅(qū)動(dòng)50個(gè)脈沖。
驅(qū)動(dòng)模塊沿著對(duì)角線移動(dòng)可以同時(shí)測(cè)量到掃描儀在上下以及左右方向中引起圖像尺寸變化的情況。
1.3.3 上下移動(dòng)掃描
使用 基于WiFi步進(jìn)電機(jī)驅(qū)動(dòng)單蝸桿滑軌帶動(dòng)陰影圈圓盤 在掃描儀上網(wǎng)上移動(dòng)。
2.1 檢測(cè)圓形
2.1.1 利用HoughCircles檢測(cè)
def modelArg(filename): img = cv2.imread(os.path.join(filedir,filename)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, param1=220, param2=30, minRadius=90, maxRadius=115) return img, circles
由于所檢測(cè)到的四個(gè)圓圈在輸出結(jié)果上并沒(méi)有進(jìn)行排序,因此通過(guò)下面程序?qū)τ谒膫€(gè)結(jié)果進(jìn)行排序。
def sortcircles(cc): cca = mean(cc[0], axis=0) angle = [math.atan2(c[1]-cca[1], c[0]-cca[0])*180/pi for c in cc[0]] angle = [s if s >= 0 else 360+s for s in angle] ar = sorted(zip(angle, cc[0].T[2])) sortr = [s[1] for s in ar] rcompare = list(array(sortr) > 100) for i in range(3): rc = roll(rcompare, i+1) rsr = roll(sortr, i+1) if rc[0] == True and rc[1] == True: break return rsr
2.1.2 檢測(cè)結(jié)果
std(r1dim): 0.6596184372901917 std(r2dim): 0.5734277367591858 std(r3dim): 0.6673048138618469 std(r4dim): 0.5788406133651733 max(r1dim)-min(r1dim): 3.3000030517578125 max(r2dim)-min(r2dim): 2.5 max(r3dim)-min(r3dim): 3.6999969482421875 max(r4dim)-min(r4dim): 3.0
#!/usr/local/bin/python # -*- coding: gbk -*- #============================================================ # PROC1.PY -- by Dr. ZhuoQing 2022-01-25 # # Note: #============================================================ from headm import * # = import cv2 from tqdm import tqdm from tqdm import tqdm scandiag = '/home/aistudio/work/Scanner/ScanDiag' scanrow = '/home/aistudio/work/Scanner/ScanRow' scanvert = '/home/aistudio/work/Scanner/ScanVert' filedir = scanrow filedim = sorted([s for s in os.listdir(filedir) if s.find('jpg') > 0]) #printt(filedim:) #------------------------------------------------------------ ''' gifpath = '/home/aistudio/GIF' if not os.path.isdir(gifpath): os.makedirs(gifpath) gifdim = os.listdir(gifpath) for f in gifdim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn) for id,f in tqdm(enumerate(filedim)): fn = os.path.join(filedir, f) img = cv2.imread(fn)[:,:,::-1] img[where(img < 50)] = 0 plt.clf() plt.figure(figsize=(8,12)) plt.imshow(img) savefile = os.path.join(gifpath, '%03d.jpg'%id) plt.savefig(savefile) ''' #------------------------------------------------------------ def modelArg(filename): img = cv2.imread(os.path.join(filedir,filename)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, param1=150, param2=40, minRadius=90, maxRadius=115) # circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, # 50, param1=220, param2=30, # minRadius=170, maxRadius=240) return img, circles #------------------------------------------------------------ def circleImg(img, cc): for c in cc[0]: cv2.circle(img, (int(c[0]), int(c[1])), int(c[-1]), (0,0,255), 12) return img #------------------------------------------------------------ def sortcircles(cc): # printt(cc[0]) cca = mean(cc[0], axis=0) angle = [math.atan2(c[1]-cca[1], c[0]-cca[0])*180/pi for c in cc[0]] angle = [s if s >= 0 else 360+s for s in angle] ar = sorted(zip(angle, cc[0].T[2])) sortr = [s[1] for s in ar] rcompare = list(array(sortr) > 100) for i in range(3): rc = roll(rcompare, i+1) rsr = roll(sortr, i+1) if rc[0] == True and rc[1] == True: break return rsr #------------------------------------------------------------ gifpath = '/home/aistudio/GIF' if not os.path.isdir(gifpath): os.makedirs(gifpath) gifdim = os.listdir(gifpath) for f in gifdim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn) allcircle = [] for id,f in tqdm(enumerate(filedim)): f = filedim[2] img, cc = modelArg(f) printt(cc:) if len(cc) > 0: img[where(img<50)] = 0 circleImg(img, cc) r = sortcircles(cc) allcircle.append(r) plt.clf() plt.figure(figsize=(8,12)) plt.imshow(img[:,:,::-1]) savefile = os.path.join(gifpath, '%03d.jpg'%id) plt.savefig(savefile) break #------------------------------------------------------------ #printt(allcircle) ''' r1dim = [c[0] for c in allcircle] r2dim = [c[1] for c in allcircle] r3dim = [c[2] for c in allcircle] r4dim = [c[3] for c in allcircle] plt.clf() plt.figure(figsize=(12,8)) plt.plot(r1dim, label='Circle1') plt.plot(r2dim, label='Circle2') plt.plot(r3dim, label='Circle3') plt.plot(r4dim, label='Circle4') plt.xlabel("n") plt.ylabel("Ratio") plt.grid(True) plt.legend(loc='upper right') plt.tight_layout() plt.savefig('/home/aistudio/stdout.jpg') plt.show() printt(std(r1dim):,std(r2dim):,std(r3dim):,std(r4dim):) printt(max(r1dim)-min(r1dim):, max(r2dim)-min(r2dim):, max(r3dim)-min(r3dim):, max(r4dim)-min(r4dim):) ''' #------------------------------------------------------------ # END OF FILE : PROC1.PY #============================================================
2.2 檢測(cè)精度問(wèn)題
在前面處理圖片過(guò)程中,由于使用了Hough算法,它在檢測(cè)圓形參數(shù)的時(shí)候,精度不是很高。這是Hough算法本身的精度問(wèn)題。因此需要在初步獲得圓形位置的基礎(chǔ)上,進(jìn)一步提高圓形半徑的檢測(cè)精度。
2.2.1 采用ContourArea函數(shù)
根據(jù)前面討論過(guò)程,為了進(jìn)一步提高圓半徑檢測(cè)精度,可以采用OpenCV中的Contour以及ContourArea的算法,通過(guò)對(duì)圖片進(jìn)行分割之后,提取黑色區(qū)域的面積從而反過(guò)來(lái)計(jì)算影響半徑。
2.2.2 對(duì)于邊緣過(guò)渡帶進(jìn)行擬合
圓形的半徑來(lái)自于邊緣過(guò)渡帶的細(xì)分,對(duì)于過(guò)渡帶的灰度變化進(jìn)行建模,使用上升沿的中點(diǎn)進(jìn)行擬合來(lái)獲取亞像素級(jí)別的半徑。
為
了提高圖片的處理速度,為后面提高高性能算法,需要對(duì)圖片進(jìn)行預(yù)處理。
3.1 圖片預(yù)分割
在測(cè)量的圖片中,圓形模板僅僅占圖片的一小部分,利用前面提取的模板中圓形的中心位置,將涵蓋后模板的圖片區(qū)域分割出來(lái),這一方面減少了圖片數(shù)據(jù)存儲(chǔ)的空間,并未后面進(jìn)行搜索和處理提高了處理速度。
3.1.1 處理程序
from headm import * # = from tqdm import tqdm import cv2 scandiag = '/home/aistudio/work/Scanner/ScanDiag' scanrow = '/home/aistudio/work/Scanner/ScanRow' scanvert = '/home/aistudio/work/Scanner/ScanVert' scandiagblock = '/home/aistudio/work/Scanner/ScanDiagBlock' scanrowblock = '/home/aistudio/work/Scanner/ScanRowBlock' scanvertblock = '/home/aistudio/work/Scanner/ScanVertBlock' filedir = scandiag outfiledir = scandiagblock filedim = sorted([s for s in os.listdir(filedir) if s.find("jpg") > 0]) def modelArg(filename): img = cv2.imread(os.path.join(filedir,filename)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) bigcircle = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, param1=200, param2=60, minRadius=530, maxRadius=580) return img, bigcircle, bigcircle BLOCK_SIDE2 = 600 for id,f in tqdm(enumerate(filedim)): if id >= 0: img,c,bc = modelArg(f) cc = bc[0][0] keepcc = cc leftid = int(cc[0] - BLOCK_SIDE2) topid = int(cc[1] - BLOCK_SIDE2) if leftid < 0: leftid = 0 if topid < 0: topid = 0 rightid = leftid + BLOCK_SIDE2*2 bottomid = topid + BLOCK_SIDE2*2 if rightid > img.shape[1]: rightid = img.shape[1] leftid = rightid - BLOCK_SIDE2*2 if bottomid > img.shape[0]: bottomid = img.shape[0] topid = bottomid - BLOCK_SIDE2*2 blockimg = img[topid:bottomid, leftid:rightid, :] outfile = os.path.join(outfiledir, f) cv2.imwrite(outfile, blockimg)
3.1.2 處理結(jié)果
根據(jù)分析圖片中金屬圖片的像素尺寸,可以看到,通過(guò)截取大圓半徑5/4倍的外接正方形,將原始圖像分離出來(lái)進(jìn)行存儲(chǔ)。
外輪廓的直徑為1105,它的5/4對(duì)應(yīng)的1381。如果截取靠近邊緣,則以邊緣為起始點(diǎn)往內(nèi)延長(zhǎng)1381,使得所有截取的存儲(chǔ)圖片的大小都是1381。
from headm import * # = from tqdm import tqdm import cv2 scandiag = '/home/aistudio/work/Scanner/ScanDiag' scanrow = '/home/aistudio/work/Scanner/ScanRow' scanvert = '/home/aistudio/work/Scanner/ScanVert' scandiagblock = '/home/aistudio/work/Scanner/ScanDiagBlock' scanrowblock = '/home/aistudio/work/Scanner/ScanRowBlock' scanvertblock = '/home/aistudio/work/Scanner/ScanVertBlock' filedir = scandiagblock outfiledir = scanrowblock filedim = sorted([s for s in os.listdir(filedir) if s.find("jpg") > 0]) gifpath = '/home/aistudio/GIF' if not os.path.isdir(gifpath): os.makedirs(gifpath) gifdim = os.listdir(gifpath) for f in gifdim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn) for id,f in tqdm(enumerate(filedim)): img = cv2.imread(os.path.join(filedir, f)) img[where(img<50)] = 0 plt.clf() plt.figure(figsize=(10,10)) plt.imshow(img[:,:,::-1]) savefile = os.path.join(gifpath, '%03d.jpg'%id) plt.savefig(savefile) plt.close()
3.2 Studio帶有GPU環(huán)境
根據(jù) AI Studio中的OpenCV在三種環(huán)境下對(duì)比 對(duì)比,在存在有GPU處理環(huán)境下,也就是在“至尊版本”中,可以獲得大約五倍速度的提升。因此對(duì)于大量數(shù)據(jù)處理的時(shí)候轉(zhuǎn)移到GPU環(huán)境下可以獲得更快的速度。
對(duì)
于噴涂有黃色油漆的金屬圓盤進(jìn)行掃描。利用 滑軌帶動(dòng)圓盤 在 V370掃描儀 上移動(dòng),利用300dpi分辨率獲得金屬圓盤模板在掃描儀上不同位置和高度的圖片,通過(guò)后期算法研究掃描儀在不同地方的失真以及影響測(cè)量準(zhǔn)確性的因素。
■ 相關(guān)文獻(xiàn)鏈接:
獲取棋盤格與標(biāo)準(zhǔn)模板在掃描儀上不同位置圖片
蝸桿滑軌
基于ESP8266 WiFi控制的步進(jìn)升降機(jī)械平臺(tái)
實(shí)驗(yàn)室中的第三只手:滑軌參數(shù)以及控制接口
AI Studio中的OpenCV在三種環(huán)境下對(duì)比
Epson Perfection V370 Photo圖片掃描自動(dòng)控制
● 相關(guān)圖表鏈接:
圖1.1.1 噴涂有黃色油漆的陰影圈金屬模板
圖1.3.1 掃描以上的金屬模板移動(dòng)距離
圖1.2.2 掃描結(jié)果
圖2.1 對(duì)角線方向移動(dòng)
圖2.1.1 對(duì)角線移動(dòng)掃描
圖3.1 上下掃描結(jié)果
圖2.1.1 在AI Studio上處理圖片
圖2.1.2 四個(gè)結(jié)果對(duì)應(yīng) 圓圈的位置
圖2.1.3 檢測(cè)結(jié)果顯示
圖2.1.4 對(duì)角線移動(dòng)測(cè)量
圖2.1.5 四個(gè)圓圈測(cè)量結(jié)果
圖3.1.1 對(duì)角線移動(dòng)掃描結(jié)果
圖3.1.2 水平移動(dòng)
圖3.1.3 對(duì)角線移動(dòng)
圖3.1.4 垂直移動(dòng)
AI 圖像處理
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。