[关闭]
@TangWill 2019-07-21T19:00:16.000000Z 字数 2058 阅读 486

梯度条形结构光编码图案生成及RGB检测

大创


  1. # -*- coding: utf-8 -*-
  2. from PIL import Image
  3. import os
  4. def de_bruijn(k, n):
  5. """
  6. de Bruijn sequence for alphabet k
  7. and subsequences of length n.
  8. """
  9. try:
  10. # let's see if k can be cast to an integer;
  11. # if so, make our alphabet a list
  12. _ = int(k)
  13. alphabet = list(map(str, range(k)))
  14. except (ValueError, TypeError):
  15. alphabet = k
  16. k = len(k)
  17. a = [0] * k * n
  18. sequence = []
  19. def db(t, p):
  20. if t > n:
  21. if n % p == 0:
  22. sequence.extend(a[1:p + 1])
  23. else:
  24. a[t] = a[t - p]
  25. db(t + 1, p)
  26. for j in range(a[t - p] + 1, k):
  27. a[t] = j
  28. db(t + 1, t)
  29. db(1, 1)
  30. return "".join(alphabet[i] for i in sequence)
  31. if __name__ == "__main__":
  32. DB = de_bruijn(3, 3)
  33. DB_list = list(DB)
  34. print(DB_list)
  35. x = 912
  36. y = 1140
  37. c = Image.new("RGB", (x, y))
  38. skip = False
  39. i = 0
  40. while i < x:
  41. for k in range(0, 15):
  42. rgb = 255 - (5*(k - 7) ** 2)
  43. temp = int(i / 20)
  44. if temp < 27:
  45. for j in range(0, y):
  46. if i % 20 == 0:
  47. for m in range(0, 5):
  48. c.putpixel([i + m + 186, j], (0, 0, 0))
  49. i = i + 5
  50. skip = False
  51. elif DB_list[temp] == "0":
  52. c.putpixel([i + k + 186, j], (rgb, 0, 0))
  53. skip = True
  54. elif DB_list[temp] == "1":
  55. c.putpixel([i + k + 186, j], (0, rgb, 0))
  56. skip = True
  57. elif DB_list[temp] == "2":
  58. c.putpixel([i + k + 186, j], (0, 0, rgb))
  59. skip = True
  60. if skip:
  61. i = i + 15
  62. else:
  63. i = i + 1
  64. c.show()
  65. c.save("structured_light .bmp")
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. from PIL import Image
  4. img = Image.open("structured_light .bmp")
  5. # 获取图片尺寸的大小(600,600)
  6. print(img.size)
  7. # 获取图片的格式 png
  8. print(img.format)
  9. # 获取图片的图像类型 RGBA
  10. print(img.mode)
  11. # 显示图片
  12. img.show()
  13. # 获取某个像素位置的值
  14. print(img.getpixel((475, 52)))
  15. # 生成缩略图
  16. # img.thumbnail((128, 128))
  17. # 保存图片
  18. # img.save('image_thumb.jpg', 'JPEG')
  19. """
  20. # 获取图片指定像素点的像素
  21. def getPngPix(pngPath = "test.png",x = 1,y = 1):
  22. img_src = Image.open(pngPath)
  23. img_src = img_src.convert('RGBA')
  24. img_array = img_src.load()
  25. data = img_array[x,y]
  26. img_src.close()
  27. return data
  28. # 输入当前位置像素 (207,75,90,255)
  29. print(getPngPix("test.png",126,52))
  30. """
  31. # 判断某像素值是否在图像中
  32. # 查找目标色值 (1,1,1,255)
  33. # 查找目标色值 (2,2,2,255)
  34. # 查找目标色值 (255,250,250,255)
  35. i = 1
  36. j = 1
  37. result = 0
  38. break_flag = False
  39. width = img.size[0] # 长度
  40. height = img.size[1] # 宽度
  41. for i in range(0, width): # 遍历所有长度的点
  42. for j in range(0, height): # 遍历所有宽度的点
  43. # 每个像素点的颜色RGBA的值(r,g,b,alpha)
  44. data = (img.getpixel((i, j))) # 打印该图片的所有点
  45. # 判断RGBA的值
  46. if (data[0] ==255 and data[1] == 0 and data[2] == 0):
  47. # if (data[0]==207 and data[1]==75 and data[2]==90):
  48. result = 1
  49. print("图片中存在该像素!")
  50. break_flag = True
  51. break
  52. if break_flag == True:
  53. break
  54. if break_flag == True:
  55. break
  56. if (result == 0):
  57. print("图片中不存在该像素!")
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注