image_file.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:file-metadata 作者: pywikibot-catfiles 项目源码 文件源码
def analyze_color_calibration_target(self):
        """
        Find whether there is a color calibration strip on top of the image.
        """
        grey_array = self.fetch('ndarray_grey')
        image_array = self.fetch('ndarray')
        if grey_array is None:
            return {}

        # For the images we're testing, the IT8 bar takes about 20% of the
        # image and also in the 20% we need the mid area
        bary = int(0.2 * grey_array.shape[0])

        def bar_intensity(x):
            sampley = max(int(0.1 * x.shape[0]), 2)
            return numpy.mean(
                x[(x.shape[0] - sampley) // 2:(x.shape[0] + sampley) // 2,
                  :, ...],
                axis=0)

        topbar = bar_intensity(grey_array[:bary, :, ...])
        botbar = bar_intensity(grey_array[-bary:, :, ...])

        def _merge_near(arr):
            out = []
            last_elem = arr[0]
            out.append(last_elem)
            for elem in arr[1:]:
                if elem != last_elem:
                    out.append(elem)
                last_elem = elem
            return numpy.asarray(out)

        # Bottom bars seem to have smaller intensity because of the background
        # Hence, we set a smaller threshold for peaks in bottom bars.
        bot_spikes = _merge_near((numpy.diff(botbar)) > -2.5).sum()
        top_spikes = _merge_near((numpy.diff(topbar)) < 3).sum()
        top_grey_mse, bot_grey_mse = 0, 0
        if image_array.ndim == 3:
            for chan in range(image_array.shape[2]):
                top_grey_mse += (
                    (image_array[bary:, :, chan] -
                     grey_array[bary:]) ** 2).mean()
                bot_grey_mse += (
                    (image_array[-bary, :, chan] -
                     grey_array[-bary]) ** 2).mean()
            top_grey_mse /= 3.0
            bot_grey_mse /= 3.0

        data = {}
        if 15 < top_spikes < 25:
            data['Color:IT8TopBar'] = top_spikes
            data['Color:IT8TopBarGreyMSE'] = top_grey_mse
        if 15 < bot_spikes < 25:
            data['Color:IT8BottomBar'] = bot_spikes
            data['Color:IT8BottomBarGreyMSE'] = bot_grey_mse

        return data
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号