フィルターのクリア

imwrite saving image incorrectly

14 ビュー (過去 30 日間)
Harry West
Harry West 2020 年 12 月 8 日
コメント済み: Ameer Hamza 2020 年 12 月 8 日
I am trying to use app designer to create an app where you can load an image from your desktop, compress or decompress the image and then save the image. I have been able to load the image without any issues however when I save the image it removes the background or makes the whole image black. Heres my code:
[file, path] = uiputfile('.png');
image = imread(app.imagePath);
imwrite(image,fullfile(path,file))
app.imagePath is the path to the original image that I want to save

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 12 月 8 日
Does your png image have transparent components? Try saving with the alpha channel.
[file, path] = uiputfile('.png');
[image, ~, amap] = imread(app.imagePath);
imwrite(image,fullfile(path,file), 'Alpha', amap)
  5 件のコメント
Harry West
Harry West 2020 年 12 月 8 日
The images with alpha seem to have been fixed however this image also appear to turn black when saved. What issue would this be?
Ameer Hamza
Ameer Hamza 2020 年 12 月 8 日
Ok, the issue is not the alpha map; rather, the png file is saved as an indexed image.
Either convert it to rgb image before using imwrite()
[img, cmap] = imread('block.png');
img_rgb = ind2rgb(img, cmap);
imwrite(img_rgb, 'test.png')
or specify colormap in imwrite()
[img, cmap] = imread('block.png');
imwrite(img, cmap, 'test.png')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by