HSV转RGB算法提速及OpenCV颜色转换异常问题咨询
图像HSV转RGB的效率与颜色异常问题
我从事图像像素处理工作,需要通过特定公式为灰度图上色,在HSV空间处理后转换为RGB格式保存。目前遇到以下问题:
- 使用OpenCV的
cv2.cvtColor或matplotlib的hsv_to_rgb进行颜色空间转换时,图像颜色出现异常; - 逐像素调用
colorir.HSV类的rgb()方法能得到正确颜色,但双重循环导致效率极低——600x600图像耗时约15秒,目标耗时需低于1秒。尝试过线程及np.apply_along_axis优化,均未达到预期效果,甚至增加了运行时间。
此外,我还想咨询:是否存在无需转换为RGB即可直接保存HSV空间图像的方法,以规避上述转换问题?
逐像素转换代码
for i in range(row): for j in range(col): final[i][j][0], final[i][j][1], final[i][j][2] = colorir.HSV(final[i][j][0], final[i][j][1], final[i][j][2], max_sva=255).rgb()
OpenCV转换报错代码
final = [[[105, 213, 235], [105, 213, 235], [105, 213, 235], [105, 213, 235], [105, 213, 235], [105, 213, 235]]] final = np.asarray(final) final = cv2.cvtColor(final,cv2.COLOR_HSV2BGR) cv2.imshow("image", final) cv2.waitKey(0)
报错信息
error (Unsupported depth of input image: 'VDepth::contains(depth)' where 'depth' is 4 (CV_32S))
转换为np.float32后颜色会失真。
内容的提问来源于stack exchange,提问作者nadia
相关产品推荐
相关产品推荐

