any one help me to correct this code or help me my I have a graduation project coming soon
Show older comments
clear; close all; clc;
warning('off', 'all');
[contentFile, contentPath] = uigetfile({'*.bmp;*.jpg;*.png;*.tif', 'ملفات الصور'}, 'اختر صورة المحتوى');
if isequal(contentFile, 0)
error('لم يتم اختيار صورة محتوى');
end
contentImage = imread(fullfile(contentPath, contentFile));
[styleFile, stylePath] = uigetfile({'*.bmp;*.jpg;*.png;*.tif', 'ملفات الصور'}, 'اختر صورة النمط');
if isequal(styleFile, 0)
error('لم يتم اختيار صورة نمط');
end
styleImage = imread(fullfile(stylePath, styleFile));
contentImage = im2double(contentImage);
styleImage = im2double(styleImage);
if size(contentImage,3) == 1
contentImage = repmat(contentImage, [1 1 3]);
end
if size(styleImage,3) == 1
styleImage = repmat(styleImage, [1 1 3]);
end
styleImage = imadjust(styleImage, [0.1 0.9], []);
styleImage = imgaussfilt(styleImage, 1);
net = vgg19();
inputSize = net.Layers(1).InputSize(1:2);
% تغيير حجم الصور مع الحفاظ على التناسب
contentImage = imresize(contentImage, inputSize);
styleImage = imresize(styleImage, inputSize);
%% 4. تحديد الطبقات الصحيحة
contentLayer = 'conv4_2';
styleLayers = {'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'};
styleFeatures = getStyleFeatures(styleImage, net, styleLayers);
numIterations = 500;
learningRate = 0.01;
styleWeight = 1e6;
dlContent = dlarray(contentImage, 'SSC');
dlTransform = dlContent;
figure;
for iter = 1:numIterations
[grad, loss] = dlfeval(@computeGradients, dlTransform, dlContent, net, ...
contentLayer, styleLayers, styleFeatures, styleWeight);
dlTransform = dlTransform - learningRate * grad;
if mod(iter,50) == 0 || iter == 1
fprintf('التكرار %d: الخسارة الكلية=%.2f, المحتوى=%.2f, النمط=%.2f\n', ...
iter, loss.total, loss.content, loss.style);
% عرض التقدم
currentImg = uint8(extractdata(dlTransform)*255);
imshow(currentImg);
title(sprintf('التكرار %d/%d', iter, numIterations));
drawnow;
end
end
outputImage = uint8(extractdata(dlTransform)*255);
[~,name,ext] = fileparts(contentFile);
outputFile = fullfile(pwd, [name '_styled' ext]);
imwrite(outputImage, outputFile);
fprintf('تم حفظ الصورة الناتجة بنجاح في: %s\n', outputFile);
function features = getStyleFeatures(styleImg, net, styleLayers)
if ~ismatrix(styleImg) && ~(ndims(styleImg)==3)
error('يجب أن تكون صورة النمط مصفوفة 2D أو 3D');
end
if size(styleImg,3) ~= 3
error('يجب أن تحتوي صورة النمط على 3 قنوات لونية (RGB)');
end
try
dlStyle = dlarray(styleImg, 'SSC');
catch
error('فشل تحويل صورة النمط إلى dlarray');
end
features = struct();
for i = 1:length(styleLayers)
layer = styleLayers{i};
try
dlFeatures = activations(net, dlStyle, layer);
features.(layer) = computeGramMatrix(dlFeatures);
catch ME
error('فشل في استخراج خصائص الطبقة %s: %s', layer, ME.message);
end
end
end
function gramMatrix = computeGramMatrix(features)
[H,W,C] = size(features);
reshaped = reshape(features, H*W, C);
gramMatrix = reshaped' * reshaped / (H*W*C);
end
function [gradients, loss] = computeGradients(dlTransform, dlContent, net, ...
contentLayer, styleLayers, styleFeatures, styleWeight)
contentFeatures = activations(net, dlContent, contentLayer);
transformContentFeatures = activations(net, dlTransform, contentLayer);
contentLoss = mean((transformContentFeatures - contentFeatures).^2);
styleLoss = 0;
for i = 1:length(styleLayers)
layer = styleLayers{i};
transformFeatures = activations(net, dlTransform, layer);
gramTransform = computeGramMatrix(transformFeatures);
gramStyle = styleFeatures.(layer);
styleLoss = styleLoss + mean((gramTransform - gramStyle).^2);
end
styleLoss = styleLoss / length(styleLayers);
totalLoss = contentLoss + styleWeight * styleLoss;
gradients = dlgradient(totalLoss, dlTransform);
loss.total = double(totalLoss);
loss.content = double(contentLoss);
loss.style = double(styleLoss);
end
%%%%
erorr
rror using styletransfer>getStyleFeatures (line 111)
فشل في استخراج خصائص الطبقة conv1_1: Invalid 2-D image data. Specify image data as a 3-D numeric array containing a single image, a
4-D numeric array containing multiple images, a datastore, or a table containing image file paths or images in the first column.
Error in styletransfer (line 48)
styleFeatures = getStyleFeatures(styleImage, net, styleLayers);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24 Comments
Image Analyst
on 27 Apr 2025
Looks like one of the arguments is not what it expects. What does this show in the command window?
whos styleImage
whos net
whos styleLayers
osama
on 28 Apr 2025
Cris LaPierre
on 28 Apr 2025
Please attach your 2 images to your post so we can test your code. You can attach them using the paperclip icon.
Walter Roberson
on 28 Apr 2025
styleImage 384x512x3 2359296 single
I wonder if the problem is that styleImage is datatype single instead of uint8 or double ?
Image Analyst
on 29 Apr 2025
@osama unless you know for a fact that your network requires floating point images, comment out these two lines
contentImage = im2double(contentImage);
styleImage = im2double(styleImage);
and see how it goes.
osama
on 30 Apr 2025
Walter Roberson
on 1 May 2025
Your code is for bmp files, but you have posted two jpg images and no bmp images.
It is not clear which image is intended to be the "Content" image.
osama
on 1 May 2025
Walter Roberson
on 1 May 2025
It is not clear which image is intended to be the "Content" image. It is not clear which images are intended to be within the style folder.
osama
on 1 May 2025
Walter Roberson
on 1 May 2025
I am running the code now. It is taking a fair while.
Walter Roberson
on 2 May 2025
Edited: Walter Roberson
on 2 May 2025
I made the qlf file into a bmp file and put it inside a directory named "styles".
I made the non-qlf file into a bmp file, and gave the name of that file in response to the uigetfile()
The code ran without producing any error messages. It tooks more than an hour to run on my machine.
At this point, I do not know what I should be looking for?
Note: I ran on R2025a Pre-release on an intel iMac.
osama
on 2 May 2025
osama
on 2 May 2025
Walter Roberson
on 3 May 2025
I do not know what to look for to determine whether there is transfer of style, but the final image did not have noticeable mixing of the other image.
I prefer to handle discussions in public.
osama
on 3 May 2025
Walter Roberson
on 3 May 2025
If I imresize() the original image to be the same size as the output image, then I am able to compare the output image to the original image. The output image has slightly different lighting compared to the input image -- a little pinker, less stark white.
Walter Roberson
on 3 May 2025
I do not understand what it means for the "style" to be "transferred".
Is the idea that you want to end up with the teeth isolated and the gums dimmed out? I don't think your current code attempts that; your current code does not attempt to isolate the teeth, and so could easily find "features" in the gums area.
osama
on 3 May 2025
Walter Roberson
on 3 May 2025
That makes it sounds as if you just want to copy the style image to the regular image.
What is it about the style image that you want merged with the regular image? Saying "the style properties" is too general.
osama
on 4 May 2025
Walter Roberson
on 5 May 2025
No-one is going to be able to assist you unless you explain what it means to you to "transfer style properties" .
Are you trying to end up with the teeth unchanged but the gums a monochrome red ?
Answers (0)
Categories
Find more on Built-In Training in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!