我整天都在做這個編程挑戰,我已經完成了一個 python 程式,對我來說,它應該在邏輯上作業并輸出正確的答案 - 但事實并非如此。我有一個朋友的程式可以輸出正確的答案,所以我有一種方法可以將我的輸出與他們的輸出進行比較,但我看不出為什么我的也不應該輸出正確答案。
挑戰是從影像中的像素中獲取密碼,第一行編號為 0..99,第二行編號為 100..199,等等。白色像素代表 ascii 代碼。特定白色像素的 ascii 代碼等于與最后一個白色像素的偏移量。例如,位置 65 處影像的第一個白色像素將表示 ASCII 碼 65 ('A'),位置 131 處的下一個白色像素將表示 (131 - 65) = 66 ('B'),依此類推。
兩個程式中用于挑戰的影像
這是我的代碼(不正確的輸出,Python 3):
import cv2
import numpy as np
# load img
im = cv2.imread('PNG.png')
# define colour
white = [255,255,255]
# get coords
Y,X = np.where(np.all(im==white,axis=2))
a = X.tolist()
# format list to string
x = str(a)
# clean string
x = x.replace(' ', '',)
x = x.replace('[', '')
x = x.replace(']', '')
# reformat CSV string to list
x = x.split(",")
# map each str to int
x = map(int, x)
# convert mapped outputs to list of ints
x = list(x)
firstX = x[0]
print()
print()
print('og list: ',x)
print()
print()
y = list(x)
y.pop(0)
y.insert(len(y),00)
print('conv list: ',y)
print()
print()
length = len(x)
repeatTime = 0
z = []
while repeatTime < 40:
outputInt = (y[0] - x[0])
z.insert(len(z),outputInt)
y.pop(0)
x.pop(0)
repeatTime = 1
z.insert(0,firstX)
print(z)
# Remove Negative Elements in List
# Using list comprehension
r = [ele for ele in z if ele > 0]
print()
print()
# printing result
print("List after filtering : " str(r))
print()
print()
print(r)
print()
print()
v = (''.join(chr(i) for i in r))
print(v)
CODE = {'A': '.-', 'B': '-...', 'C': '-.-.',
'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..',
'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-',
'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}
CODE_REVERSED = {value:key for key,value in CODE.items()}
def from_morse(s):
return ''.join(CODE_REVERSED.get(i) for i in s.split())
print(from_morse(v))
我的輸出:
----.. - -..-- .....--.
-invalid morse code-
這是我朋友的代碼(正確的輸出,Python 2.7)
from PIL import Image
img_file = Image.open("PNG.png")
img_width, img_height = img_file.size
img_pixel = img_file.load()
preposition=0
morse_code=""
answer=""
char_morse_dict={
'A':'.-','B':'-...','C':'-.-.','D':'-..','E':'.','F':'..-.',
'G':'--.','H':'....','I':'..','J':'.---','K':'-.-','L':'.-..',
'M':'--','N':'-.','O':'---','P':'.--.','Q':'--.-','R':'.-.',
'S':'...','T':'-','U':'..-','V':'...-','W':'.--','X':'-..-',
'Y':'-.--','Z':'--..','0':'-----','1':'.----','2':'..---','3':'...--',
'4':'....-','5':'.....','6':'-....','7':'--...','8':'---..','9':'----.',
'.':'.-.-.-',',':'--..--','?':'..--..',"'":'.----.','/':'-..-.','(':'-.--.-',
')':'-.--.-',':':'---...',';':'-.-.-.','=':'-...-',' ':'.-.-.','-':'-....-',
'_':'..--.-','"':'.-..-.','$':'...-..-','':''
}
# fetch ASCII code from image
for y_point in range(img_height) :
for x_point in range(img_width) :
if img_pixel[x_point, y_point] == 1 :
# convert ASCII code to "dits" and "dahs" in morse code
symbol = chr(x_point 100 * y_point - preposition)
if symbol != ' ' :
morse_code = symbol
else :
# decode morse code to character
char = [key for key, value in char_morse_dict.items() if value == morse_code][0]
answer = char
morse_code = ""
preposition=x_point 100 * y_point
# bye bye
print(answer)
他的輸出:
--.- --... .-. --... --- ..... .-. --... ..- ..-
Q7R7O5R7UU
我已經瀏覽了我的幾個小時,但終其一生都無法弄清楚出了什么問題。我知道在將像素位置值轉換為莫爾斯的代碼部分發生了一些事情,但我看不出問題究竟是什么。
如果有人可以幫助我指出出了什么問題,以便我可以調整我的代碼并從我的錯誤中吸取教訓,希望我的技能得到提高,我將非常感激。
uj5u.com熱心網友回復:
你計算:
Y,X = np.where(np.all(im==white,axis=2))
但您從不使用Y, 只是X這樣您就可以有效地忽略每個像素所在的行。如果一個像素位于第 Y 行、第 X 列,您應該像這樣分配它的位置:
offset = (Y * imageWidth) X
uj5u.com熱心網友回復:
如果我正確理解您想要做什么,那就是找到影像中每個白色像素的偏移量,然后計算它們之間的差異。
這可以用簡潔來完成np.diff與prepend爭論。請注意,要計算偏移量,您需要乘以y影像的寬度。
import numpy as np
def decode_message(image):
_, width, _ = im.shape
y, x = np.where(np.all(image == 255, axis=2))
offsets_from_start = width * y x
differences = np.diff(offsets_from_start, prepend=[0])
return ''.join(chr(d) for d in differences)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322022.html
標籤:Python 蟒蛇-3.x 麻木的 python-2.7 蟒蛇成像库
