亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 numpy 進行圖像翻譯

使用 numpy 進行圖像翻譯

森欄 2023-05-16 14:33:32
我想執行一定量的圖像轉換(垂直和水平移動圖像)。問題是,當我將裁剪后的圖像粘貼回畫布時,我只得到一個白色的空白框。任何人都可以在這里發現問題嗎?非常感謝img_shape = image.shape# translate image# percentage of the dimension of the image to translatetranslate_factor_x = random.uniform(*translate)translate_factor_y = random.uniform(*translate)# initialize a black image the same size as the imagecanvas = np.zeros(img_shape)# get the top-left corner coordinates of the shifted imagecorner_x = int(translate_factor_x*img_shape[1])corner_y = int(translate_factor_y*img_shape[0])# determine which part of the image will be pastedmask = image[max(-corner_y, 0):min(img_shape[0], -corner_y + img_shape[0]),             max(-corner_x, 0):min(img_shape[1], -corner_x + img_shape[1]),             :]# determine which part of the canvas the image will be pasted ontarget_coords =  [max(0,corner_y),                    max(corner_x,0),                    min(img_shape[0], corner_y + img_shape[0]),                    min(img_shape[1],corner_x + img_shape[1])]# paste image on selected part of the canvascanvas[target_coords[0]:target_coords[2], target_coords[1]:target_coords[3],:] = masktransformed_img = canvasplt.imshow(transformed_img)這就是我得到的:
查看完整描述

2 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

對于圖像翻譯,您可以使用有點晦澀的numpy.roll功能。在這個例子中,我將使用白色畫布,這樣更容易可視化。


image = np.full_like(original_image, 255)

height, width = image.shape[:-1]

shift = 100


# shift image

rolled = np.roll(image, shift, axis=[0, 1])

# black out shifted parts

rolled = cv2.rectangle(rolled, (0, 0), (width, shift), 0, -1)

rolled = cv2.rectangle(rolled, (0, 0), (shift, height), 0, -1)

如果你想翻轉圖像使黑色部分在另一邊,你可以同時使用np.fliplr和np.flipud。


結果:

http://img1.sycdn.imooc.com//64632406000188d205970379.jpg

查看完整回答
反對 回復 2023-05-16
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

tx這是一個簡單的解決方案,它僅使用數組索引按像素轉換圖像ty,不會翻轉,并且還可以處理負值:


tx, ty = 8, 5  # translation on x and y axis, in pixels

N, M = image.shape

image_translated = np.zeros_like(image)


image_translated[max(tx,0):M+min(tx,0), max(ty,0):N+min(ty,0)] = image[-min(tx,0):M-max(tx,0), -min(ty,0):N-max(ty,0)]  


例子:

http://img1.sycdn.imooc.com//6463241400010be003450123.jpg

(請注意,為簡單起見,它不處理tx > M或 的情況ty > N)。



查看完整回答
反對 回復 2023-05-16
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號