50761
ssd_kerasのサンプルコードssd.ipynbです。  
猫を検出した時だけ、ファイルCatに保存したいのですが、  
グレースケールのまま保存されてしまいます。

```

    import cv2
    import keras
    from keras.applications.imagenet_utils import preprocess_input
    from keras.backend.tensorflow_backend import set_session
    from keras.models import Model
    from keras.preprocessing import image
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.misc import imread
    import tensorflow as tf
    
    from ssd import SSD300
    from ssd_utils import BBoxUtility
    
    %matplotlib inline
    plt.rcParams['figure.figsize'] = (8, 8)
    plt.rcParams['image.interpolation'] = 'nearest'
    
    np.set_printoptions(suppress=True)
    
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.45
    set_session(tf.Session(config=config))
    
    voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
                   'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
                   'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',
                   'Sheep', 'Sofa', 'Train', 'Tvmonitor']
    NUM_CLASSES = len(voc_classes) + 1
    
    input_shape=(300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('weights_SSD300.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)
    
    inputs = []
    images = []
    img_path = './pics/fish-bike.jpg'
    img = image.load_img(img_path, target_size=(300, 300))
    img = image.img_to_array(img)
    images.append(imread(img_path))
    inputs.append(img.copy())
    img_path = './pics/cat.jpg'
    img = image.load_img(img_path, target_size=(300, 300))
    img = image.img_to_array(img)
    images.append(imread(img_path))
    inputs.append(img.copy())
    img_path = './pics/boys.jpg'
    img = image.load_img(img_path, target_size=(300, 300))
    img = image.img_to_array(img)
    images.append(imread(img_path))
    inputs.append(img.copy())
    img_path = './pics/car_cat.jpg'
    img = image.load_img(img_path, target_size=(300, 300))
    img = image.img_to_array(img)
    images.append(imread(img_path))
    inputs.append(img.copy())
    img_path = './pics/car_cat2.jpg'
    img = image.load_img(img_path, target_size=(300, 300))
    img = image.img_to_array(img)
    images.append(imread(img_path))
    inputs.append(img.copy())
    inputs = preprocess_input(np.array(inputs))
    
    preds = model.predict(inputs, batch_size=1, verbose=1)
    
    results = bbox_util.detection_out(preds)
    
    %%time
    a = model.predict(inputs, batch_size=1)
    b = bbox_util.detection_out(preds)
    
    for i, img in enumerate(images):
        # Parse the outputs.
        det_label = results[i][:, 0]
        det_conf = results[i][:, 1]
        det_xmin = results[i][:, 2]
        det_ymin = results[i][:, 3]
        det_xmax = results[i][:, 4]
        det_ymax = results[i][:, 5]
    
        # Get detections with confidence higher than 0.6.
        top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]
    
        top_conf = det_conf[top_indices]
        top_label_indices = det_label[top_indices].tolist()
        top_xmin = det_xmin[top_indices]
        top_ymin = det_ymin[top_indices]
        top_xmax = det_xmax[top_indices]
        top_ymax = det_ymax[top_indices]
    
        colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
    
        plt.imshow(img / 255.)
        currentAxis = plt.gca()
    
    　　cat_count = 0
        for i in range(top_conf.shape[0]):
            xmin = int(round(top_xmin[i] * img.shape[1]))
            ymin = int(round(top_ymin[i] * img.shape[0]))
            xmax = int(round(top_xmax[i] * img.shape[1]))
            ymax = int(round(top_ymax[i] * img.shape[0]))
            score = top_conf[i]
            label = int(top_label_indices[i])
            label_name = voc_classes[label - 1]
            display_txt = '{:0.2f}, {}'.format(score, label_name)
            coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
            color = colors[label]
            currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
            currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
        cat_count += label_name.count('Cat')
    
        plt.show()
    
        if cat_count == 1:
            cv2.imwrite('./Cat/''cat'+str(i)+'.jpg', img) 
    
```
CC BY-SA 4.0
2018-11-29T07:43:29.883
50747
2018-11-29T11:49:23.533
2018-11-29T07:49:44.890
question
python opencv
物体検出したときに画像を保存したいが、グレースケールのままになる
* 画像の読み込み => `scipy.misc.imread` : RGB (赤緑青)
  * 画像の書き出し => `cv2.imwrite` : BGR（青緑赤）

とモジュールごとに色素空間の扱いが異なることから、opencvがBとRを逆にして画像を保存していることが原因です。  
RGBからBGRに画像を変換してから保存すると大丈夫です。

```

    if cat_count == 1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)  # RGBからBGRに変換する
        cv2.imwrite('./Cat/''cat'+str(i)+'.jpg', img) 
    
```

参考：  
<http://lang.sist.chukyo-u.ac.jp/classes/OpenCV/py_tutorials/py_gui/py_image_display/py_image_display.html>  
（一番最後のWarning欄）

* * *

補足

この画像をscipy.misc.imreadで読んだ状態

[![最初の画像](https://i.stack.imgur.com/7e4PR.jpg)](https://i.stack.imgur.com/7e4PR.jpg)

上の状態でcv2.imwriteを使ってそのまま書き出した状態

[![cv2.imwriteした画像](https://i.stack.imgur.com/5tC06.jpg)](https://i.stack.imgur.com/5tC06.jpg)

cv2.cvtColor(img, cv2.COLOR_RGB2BGR)で変換してから書き出した状態

[![変換後](https://i.stack.imgur.com/0CMpv.jpg)](https://i.stack.imgur.com/0CMpv.jpg)
CC BY-SA 4.0
2018-11-29T11:28:35.183
50761
2018-11-29T11:49:23.533
2018-11-29T11:49:23.533
26558
26558
50747
answer