any one help me to correct this code or help me my I have a graduation project coming soon

4 vues (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 5 Mai 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 ?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by